Nika Roffy
Nika Roffy

Reputation: 941

Pass props from react router Link in reactJS

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>

Upvotes: 2

Views: 130

Answers (1)

Giovanni Esposito
Giovanni Esposito

Reputation: 11156

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.

Upvotes: 2

Related Questions