【问题标题】:How to redirect to another page and use props in reactjs如何重定向到另一个页面并在 reactjs 中使用道具
【发布时间】:2020-03-11 11:58:07
【问题描述】:

在我设置的路线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) 但未定义

【问题讨论】:

    标签: javascript node.js reactjs mongodb redirect


    【解决方案1】:

    语法错误。尝试如下:

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

    并采用如下参数:

    this.props.location.state.CourseName
    

    【讨论】:

    • 我按照你说的做了,但是我得到了“无法读取未定义的属性 'CourseName'”错误
    【解决方案2】:

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

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

    道具应该在:

    props.CourseName

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-31
      • 2022-01-04
      • 2020-06-15
      • 2020-01-26
      • 2018-01-04
      • 2015-03-12
      • 2011-02-22
      • 2011-06-19
      相关资源
      最近更新 更多