【问题标题】:Pass data from CLASS component to FUNCTIONNAL component REACT将数据从 CLASS 组件传递到 FUNCTIONAL 组件 REACT
【发布时间】:2020-09-23 20:00:21
【问题描述】:

我想传递一些数据,但无法在我的功能组件中访问它。

一旦用户在我的 Class 组件上提交登录信息:

return (
  <Redirect
    to={{
      pathname: '/home',
      userName: this.state.name,
      state: { userName: this.state.name },
    }}
    userName={this.state.name}
  />
);

我想在功能组件中获取userName 数据但无法访问它,我尝试了this.props 等(但它不是一个类)并且所有道具、位置、历史等都返回undefined

【问题讨论】:

  • 阅读functional components here。功能组件接收props 作为其唯一参数。所以你会这样声明它function MyComponent(props)。然后将其用作变量,而不是this

标签: reactjs react-router


【解决方案1】:

好的,我之前没有正确使用它.. 浪费了这么多时间,但每天都在学习,非常感谢@Brian Thompson 的回答,我链接了他给出的文档: Functional components

【讨论】:

    猜你喜欢
    • 2022-06-30
    • 2018-01-06
    • 2015-04-01
    • 1970-01-01
    • 2019-08-16
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多