2

How can I pass props to another component using React router link?

I tried to do it like this. However, it is not working. By this I mean props are not passing through. I want to pass information of currencies to another component and than use It. Any suggestions?

return <Div>
        <div className="header">
            <Heading>Welcome To CoinValue </Heading>
        </div>
        <Ul>
            {currentItems.map(currencies => {
                return <Link
                    key={uuidv4()}
                    to={`/cryptoValute/${currencies.id}`
                    props={currencies}}><Li>{currencies.name}</Li></Link>
            })}
            <div>
                {this.renderPagination()}
            </div>
        </Ul>
    </Div>
4
  • Could you please give a better description of what you look to accomplish with this code? You could simply pass the props to another component without needing to use <Link />. Commented Jul 27, 2020 at 13:43
  • Does this answer your question? Pass props in Link react-router Commented Jul 27, 2020 at 13:44
  • @AlbertoPerez I especially need to pass props through the link Commented Jul 27, 2020 at 13:45
  • @k-wasilewski I'm trying that way but still cant make it tho Commented Jul 27, 2020 at 13:46

1 Answer 1

2

Ciao, try to modify your code like this:

return <Div>
    <div className="header">
        <Heading>Welcome To CoinValue </Heading>
    </div>
    <Ul>
        {currentItems.map(currencies => {
            return <Link
                key={uuidv4()}
                to={{pathname: `/cryptoValute/${currencies.id}`, query: {currencies}}}><Li>{currencies.name}</Li></Link>
                
        })}
        <div>
            {this.renderPagination()}
        </div>
    </Ul>
</Div>

Then in your component, to retrieve currencies value you could do this.props.location.query.currencies.

Sign up to request clarification or add additional context in comments.

2 Comments

Its giving me synax error on this line to={{pathname: /cryptoValute/${currencies.id}, query: {currencies}}}>`
Oh, sorry I wrongly put one ` after query. I updated my answer. Check now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.