繁体   English   中英

如何重定向到另一个页面并在 reactjs 中使用道具

[英]How to redirect to another page and use props in reactjs

在路线中我设置了props

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

在主页中,我为重定向编写了此代码:

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

GetFilePage组件中我写在 componentDidMount console.log(this.props.location.CourseName)但未定义

把你的道具像这样传给孩子们:

<Route 
  path="/login" 
  render={(props) => <GetFilePage {...props} CourseName={props.CourseName}/>} 
/>

和道具应该可以在:

props.CourseName

语法错误。 尝试如下:

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

并采用如下参数:

this.props.location.state.CourseName

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM