【问题标题】:How to pass props to this.props.children using React Router 4 with switch?如何使用带开关的 React Router 4 将道具传递给 this.props.children?
【发布时间】:2017-11-22 19:38:40
【问题描述】:

我使用 react router 4 进行如下嵌套

<MainComponent>
  <ChildComponent></ChildComponent>
  <ChildComponent></ChildComponent>
  <ChildComponent></ChildComponent>
<MainComponent>

在主要组件页面上,我有一些状态,我想将其作为道具传递给 this.props.children。 在主组件渲染方法中,我编写了 {this.props.children} 来访问子组件。现在如何将我的 CustomProps 传递给 this.props.children

提前感谢您的帮助

【问题讨论】:

标签: reactjs react-router


【解决方案1】:

假设你的状态是day:monday.那么你需要将状态传递给child as

<MainComponent>
  <ChildComponent day={this.state.day}/>

<MainComponent>

在您的子组件 ChildComponent 中,您可以使用

this.props.day

【讨论】:

  • 但在渲染方法的主要组件中,我正在传递 {this.props.children} 所以我想知道如何使用 ES6 中的 React Router 4 将自定义道具传递给 {this.props.children}
猜你喜欢
  • 2017-11-19
  • 2019-01-07
  • 2018-04-25
  • 2018-07-17
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
相关资源
最近更新 更多