【问题标题】:Passing props to component in react router [duplicate]将道具传递给反应路由器中的组件[重复]
【发布时间】:2018-08-22 03:13:15
【问题描述】:

我有一个模态框,我想在其中放置一个动态嵌套视图。我认为这样做的一个好方法是像这样使用Switch

<div class="modal-content">
  <Switch>
    <Route path="/modal/slide-1" component={ Widget1 }
    <Route path="/modal/slide-2" component={ Widget2 }
  </Switch>
</div>

我想像通常在 React 中那样将一些道具传递给这些组件

<Widget1
  prop1="Some Value"
  prop2="Some Other Value"
/>

最终我将在状态中存储/获取这些属性,但鉴于当前的实现,在定义 Switch component 时可以包含属性吗?

【问题讨论】:

  • 为什么有patch应该是path
  • @Omar 错字,谢谢
  • 如果您对此有任何问题,请告诉我们。
  • @MayankShukla 没问题,我最初没有找到这个问题。谢谢!

标签: reactjs


【解决方案1】:
 <Route
  path={"/modal/slide-1"}
  render={() => (
   <Widget1 prop1={this.state.prop1} />
  )}
 />

Render 允许您将任何道具传递给您正在渲染的组件。

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2017-10-01
    • 2018-06-17
    • 2018-08-28
    相关资源
    最近更新 更多