inconnu
inconnu

Reputation: 769

React - Passing variable to path in redirect

I have a code that redirects to a default path if a certain flag is set. The redirected URL contains a variable id. I tried

return <Redirect to = {'/products/:id/details'}/>

and

return <Redirect to = {'/products/${id}/details'}/>

but neither of them will insert the id to the URL.

This is my code:

if (flag)
    return <Redirect to = {'/products/:id/details'}/>

return(
    //renders something
)

Upvotes: 0

Views: 411

Answers (1)

Viet
Viet

Reputation: 12787

Just update like this with Template_literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals?retiredLocale=vi

 return <Redirect to = {`/products/${id}/details`}/>

Upvotes: 1

Related Questions