【发布时间】:2020-10-24 07:01:53
【问题描述】:
我在 react v16.13.1 项目中使用 react-router-dom v5.2.0,我使用静态路由将 props 从父组件传递给孙组件,父组件从其父组件(即祖父)接收它。从 App.js -> Applayout.js 接收 props 并发送到 -> APIData (last route)
class AppLayout extends Component{
constructor(props){
super(props);
}
render(){
console.log('Applayout'+' '+ this.props.data[1]);
return(
<Router>
<div>
<Navbar/>
<div className={classes.main}>
<Sidebar/>
<div className = {classes.pages}>
<Route path='/' exact component={Dashboard}/>
<Route path='/toolsandutils' exact component={ToolsandUtils}/>
<Route path='/failures' component={Failures}/>
<Route path='/osshare' component={OSshare}/>
<Route path='/employee' component={Employee}/>
<Route path='/apidata' render={props=> <APIData data={this.props.data}/>}/>
</div>
</div>
</div>
</Router>
)
}
}```
【问题讨论】:
-
你看到
Applayout的控制台输出了吗? -
@Siddharth 不,我添加这个是为了查看数据是否进入 AppLayout 并且一切正常。在调试模式下,当我看到控制权传递给 APIData 时,道具不保存任何数据。
-
同样的事情发生在我身上。使用 react v16.13.1 和 react-router v5.2.0。我无法使用 Route 的 render 方法传递任何自定义道具。
<Route path="/foo" render={props => <MyComponent thisIsNotPassed={true} {...props} /> } />。this.props.thisIsNotPassed未定义。 @victorlazlow - 你能弄明白吗?
标签: javascript reactjs react-router-dom