如何解决错误:超出最大更新深度?

时间:2019-08-14 06:23:12

标签: reactjs react-native

2周前,我开始学习React Native,并进入了“安装插件”一章。我安装了 react-native-popover-view ,但我不知道,但是对我来说,我得到了错误:

Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

我在周围搜索了两天后,我知道我在render()中有setState,因为我在App的render()中有Modal。我试图弄清楚如何更改它,但没有成功。

所以我有Modal.js类:

class Modal extends Component {
  state = {
    isVisible: true
  }

  closePopover() {
    this.setState({ isVisible: false });
  }

  render() {
    return (
      <View >
        <Popover
          isVisible={this.state.isVisible}
          fromView={this.touchable}
          onRequestClose={() => this.closePopover()}>
          <View style={styles.closeXContainer}>
              <Image source={closeX} style={styles.closeX} onPress={this.closePopover()} />
            </View>
          <Text>I'm the content of this popover!</Text>
        </Popover>
      </View>
    );
  }
}

在App.js中,当页面加载时,模态应该首先开始,覆盖App.js组件,该组件可以在背面看到。

class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Modal />
      </View>
    );
  }
}

有人可以帮忙吗?该如何避免不再出现该错误的代码?

2 个答案:

答案 0 :(得分:5)

此错误是由于在Image上调用onPress={this.closePopover()}时引起的,它会调用函数并导致应用重新渲染,这再次导致函数被调用,从而导致无限循环。最好的方法是不带括号onPress={this.closePopover}直接调用它,或者创建一个仅执行一次的匿名函数onPress={() => this.closePopover()}

答案 1 :(得分:1)

您的问题在于调用函数Widget build(BuildContext context) { return new StreamBuilder( stream: Firestore.instance.collection('COLLECTION_NAME').document('TESTID1').snapshots(), builder: (context, snapshot) { if (!snapshot.hasData) { return new Text("Loading"); } var userDocument = snapshot.data; return new Text(userDocument["name"]); } ); } 来修复它,您只需将其更改为:

rule fakegoogleanalytics : fga {
    meta:
        description = "Fake Google Analytics"
        thread_level = 6
        in_the_wild = true
    strings:
        $a = "googlc-analytics"
        $b = "googlc-analytics.cm"
        $c = "googlc-analytics.cm/v"
        $d = "googlc-analytics.cm/v/analytics.js"
        $e = ".cm"
        $f = "analytics.js"
    condition:
        $a or $b or $c or $d or $e or $f
}

希望获得帮助。