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>
Nika Roffy
  • 891
  • 2
  • 8
  • 20

1 Answers1

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.

Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30