Reactjs 通过react路由器传递道具在componentDidMount中不可用?
我已经通过react路由器将道具传递给了上述组件:Reactjs 通过react路由器传递道具在componentDidMount中不可用?,reactjs,asynchronous,react-router,Reactjs,Asynchronous,React Router,我已经通过react路由器将道具传递给了上述组件: <BrowserRouter> <Switch> <Route path="/product-page" exact render={(props) => ( <ShopPages {...props} response={this.state.response}/>)}/> </Switch> </BrowserRout
<BrowserRouter>
<Switch>
<Route path="/product-page" exact render={(props) => ( <ShopPages {...props} response={this.state.response}/>)}/>
</Switch>
</BrowserRouter>
propthis.props.response
可在组件I can console.log in The render()中使用。我不确定为什么在上面的场景中,控制台显示阵列为空。我已尝试查看数据是否可用,然后将数据显示为soconsole.log('show',this.props.response&&this.props.response)
或通过添加async
来显示:
async componentDidMount() {
const data = await this.props.response
}
但这也没什么作用。有什么帮助吗?我只是假设
this.state.response
不是承诺,但实际上是数据,因为您说它是一个空数组,因此async/await将不起任何作用
ComponentDidMount()仅在组件装载时触发一次,而不是根据装载后可能发生的道具更新执行操作的地方。因此,我建议通过承诺,如果这不是一个选项,那么做类似的事情
componentDidUpdate(prevProps, prevState) {
if (!prevProps.response.length && this.props.response.length) {
//Your code
}
}
移除渲染它的
组件周围的()
,查看它是否更改????
componentDidUpdate(prevProps, prevState) {
if (!prevProps.response.length && this.props.response.length) {
//Your code
}
}