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

在我设置的路线中props:


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

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


return <Redirect to={{

          pathname: '/getFilePage',

          CourseName:"C++"

       }}/>

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


炎炎设计
浏览 136回答 2
2回答

GCT1015

语法错误。尝试如下:return <Redirect to={{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pathname: '/getFilePage',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state:{CourseName:"C++"}&nbsp; &nbsp; &nbsp; &nbsp;}}/>并采用如下参数:this.props.location.state.CourseName

隔江千里

像这样把你的道具传递给孩子们:<Route&nbsp;&nbsp; path="/login"&nbsp;&nbsp; render={(props) => <GetFilePage {...props} CourseName={props.CourseName}/>}&nbsp;/>和道具应该可以在以下位置获得:props.CourseName
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript