我想将一些 Prop 传递给 IndexRoute 上的组件。下面是我的代码片段:
render(root: Element) {
const { store, params } = this as any;
ReactDOM.render(
<Provider {...{ store }} >
<Router history={browserHistory}>
<Route path={window.location.pathname} component={App}>
<IndexRoute component={Step1} />
<Route path="step1" component={() => (<Step1 params={params} />)} />
<Route path="step1" component={() => (<Step2 params={params} />)} />
</Route>
</Router>
</Provider>
, root);
}
//App Component
import * as React from 'react';
export var App: any = ({children}) => (
<div>
<div>{children}</div>
</div>
)
在初始加载时,我可以将 step1 作为子项加载,但我想将一些 Prop 从路由部分传递到组件。
我怎样才能得到这个?
请指导我。
谢谢, 维杰
最佳答案
使用 cloneWithProps()
克隆元素
var newStep1 = cloneWithProps(Step1, {prop1: 'value', prop2: 'value'});// add props
并将其传递给<IndexRoute />
<IndexRoute component={newStep1} />
这应该有效..
关于reactjs - 如何使用 react-router 通过路由将 props 传递给 react 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39797657/