【发布时间】:2019-10-30 08:47:25
【问题描述】:
我在通过 React 元素(如 Switch 和 Route)传递道具时遇到问题。在下面的示例中,我想将 Dashboard 组件的所有道具传递给 Account 组件。有没有办法做到这一点?
App.js
<Dashboard>
<Switch>
// Dashboard props to Account component
<Route path="/account" render={props => <Account {...props} /> } exact />
<Route path="/someothercomponent" component={Someothercomponent} />
</Switch>
</Dashboard>
Dashboard.js
render() {
const children = React.Children.map(this.props.children, child => {
var router = React.cloneElement(child, { image: this.state.image });
return router;
// Like this the router Element does receive the image prop from
// the Dashboard component. Now this image prop needs to be
// passed on to the Account component.
}
【问题讨论】: