简体   繁体   English

在 componentDidUpdate 错误中反应本机调用 setState

[英]react native call setState within componentDidUpdate error

I've a multiscreen app using react-navigation and passing a prop from CameraScreen to HomeScreen called 'barcode' like so:我有一个使用 react-navigation 的多屏应用程序,并将一个名为“条形码”的道具从 CameraScreen 传递到 HomeScreen,如下所示:

takePicture() {
        //Redirect 
        this.props.navigation.navigate('Home', {barcode: true});
    }

Source: https://reactnavigation.org/docs/en/params.html资料来源: https://reactnavigation.org/docs/en/params.html

On home screen I'm listening for the prop update within componentDidMount and if barcode is true, setState to render a new component like so:在主屏幕上,我正在监听 componentDidMount 中的道具更新,如果条形码为真,则 setState 呈现一个新组件,如下所示:

 componentDidUpdate(){
    if(this.props.navigation.getParam('barcode') === true) {
      this.setState({
        barcodeActive: true
      })
    }
  }

I get the error Invariant Violation: Maximum update depth exceeded - from calling setState within componentDidMount.我收到错误Invariant Violation: Maximum update depth exceeded - 从 componentDidMount 中调用 setState。 I understand it's in an infinite loop with no break.我知道它处于无限循环中,没有中断。

How can I listen for when this prop is passed to setState or is there a better way to achieve what I'm after?当这个道具传递给 setState 或者有更好的方法来实现我所追求的目标时,我该如何监听?

You can use prevProps argument, and then compare current and previous values of barcode .您可以使用prevProps参数,然后比较barcode的当前值和以前的值。 If they are not the same, then you can update your state.如果它们不一样,那么您可以更新您的 state。

 componentDidUpdate(prevProps){
    if(this.props.navigation.getParam('barcode') !== prevProps.navigation.getParam('barcode')) {

      if( this.props.navigation.getParam('barcode')) {
        this.setState({
          barcodeActive: true
        })
       }

    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactJS - 如何在 componentDidUpdate 中设置状态? - ReactJS - How to setstate within componentDidUpdate? 在 componentDidUpdate 内反应 setState 导致无限循环 - React setState inside componentDidUpdate causing infinite loop 如何使用带有 componentDidUpdate 和 setState 的 react js 更新 state? - How to update state with react js with componentDidUpdate and setState? 如何修复“超出最大更新深度。在 componentWillUpdate 或 componentDidUpdate 中调用 setState。” 反应错误? - How to fix “Maximum update depth exceeded.calls setState inside componentWillUpdate or componentDidUpdate.” error in react? 在setInterval()中反应本机setState,然后setInterval执行错误 - react native setState in setInterval(), then setInterval executes error setState 循环遍历一组道具 - React ComponentDidUpdate - setState while looping through an array of props - React ComponentDidUpdate react native中componentDidMount和componentDidUpdate有什么区别 - what is difference between componentDidMount and componentDidUpdate in react native 我应该在React 16的componentDidUpdate中使用setState条件更新吗? - Should I use setState conditional updates in componentDidUpdate in React 16? 为什么在 componentDidUpdate 中使用 setState 在 React 中被视为不好的做法? - Why is using setState inside componentDidUpdate seen as a bad practice in React? 在本机反应中调用 setState function 是否仅在箭头 function 中有效? - Does call to setState function in react native, only works in arrow function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM