【发布时间】: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