Ad

How To Redirect To Another Page And Use Props In Reactjs

in route i set props:

<Route path="/getFilePage" exact component={GetFilePage} CourseName={props.CourseName} />

in main page i write this code for redirect:

return <Redirect to={{
          pathname: '/getFilePage',
          CourseName:"C++"
       }}/>

in GetFilePage component i write in componentDidMount console.log(this.props.location.CourseName) but is undefined

Ad

Answer

syntax is wrong. Try like below:

return <Redirect to={{
          pathname: '/getFilePage',
          state:{CourseName:"C++"}
       }}/>

And take params like below:

this.props.location.state.CourseName
Ad
source: stackoverflow.com
Ad