Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

reactjs - How to pass in props to Link Router?

Hello I want to pass in props to another component through Link Router

I am using Class Component

  constructor(props: IBanner) {
    super(props);
    this.state = {

      jobCategories: [],
      jobKeyword: "",
      jobLocation: "",
   };


  } 

and here is my Link Router

 <Link {{
                pathname: '/search-results',
                state: {
                  jobKeyword: this.state.jobKeyword, jobLocation: this.state.jobLocation
                  }
                  }}>
                <li className="ktp-banner-submit">
                  {" "}
                  <input type="submit" value="" />{" "}
                  <i className="ktp-icon ktp-search"></i>{" "}
                </li>
                </Link>

However I am getting an error of Property 'to' is missing in type '{ children: Element; pathname: string; state: { jobKeyword: string; jobLocation: string; }; }' but required in type 'LinkProps'

What error is this and what can i do to mitigate this/

question from:https://stackoverflow.com/questions/65602530/how-to-pass-in-props-to-link-router

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You forgot to attribute. Try this:

<Link
  to={{
    pathname: '/search-results',
    state: {
      jobKeyword: this.state.jobKeyword, jobLocation: this.state.jobLocation
    }
  }}
/>

You cannot do something like this in React:

<Component {obj} />

You need to assign that object to an attribute:

<Component p={obj} />

Or you can destruct the object(attributes):

<Component ...{{name:'foo', lastName: 'bar'}} />

Which translates to:

<Component name='foo' lastName='bar' />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...