Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 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/


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

1 Answer

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' />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...