【发布时间】:2020-04-28 13:11:46
【问题描述】:
我之前使用过下面这样的到达路由器,它工作正常
.....
<Router>
<ComponentA path="/:id">
<ComponentB path="/">
<Router>
....
我决定用上下文重构我的代码,代码被重构成这样:
<GlobalContextProvider>
<GlobalContext.Consumer>
{( context) =>{
return(
.....
<Router>
<ComponentA path="/:id">
<ComponentB path="/">
<Router>
....
}
重构后,ComponentA 无法正常工作,因为 url 参数 prop id 没有传递
在 ComponentA.js 中,像这样测试:
componentDidMount() {
const { id } = this.props;
console.log(id); // return undefined
}
当我 console.log(this.props) 时,它返回与 this.context 相同的结果
有人能帮我理解为什么会这样吗?如何正确重构上下文?
非常感谢
【问题讨论】:
标签: reactjs react-context reach-router