我有一个使用反应导航的多屏应用程序,并将一个从CameraScreen传递到HomeScreen的道具称为“条形码”,如下所示:
takePicture() {
//Redirect
this.props.navigation.navigate('Home', {barcode: true});
}
来源:https://reactnavigation.org/docs/en/params.html
在主屏幕上,我正在侦听componentDidMount中的道具更新,如果条形码为true,则setState像这样渲染一个新组件:
componentDidUpdate(){
if(this.props.navigation.getParam('barcode') === true) {
this.setState({
barcodeActive: true
})
}
}
我收到错误Invariant Violation: Maximum update depth exceeded
-通过在componentDidMount中调用setState。我知道它处于无限循环中,没有间断。
当此道具传递给setState时,或者在实现我所追求的目标时有更好的方法,我该如何听?]
我有一个使用反应导航的多屏应用程序,并将一个摄影机道具从CameraScreen传递到HomeScreen,称为'barcode',如下所示:takePicture(){//重定向this.props.navigation.navigate('...
您可以使用prevProps
参数,然后比较barcode
的当前值和先前值。如果它们不相同,则可以更新状态。