【问题标题】:Error with invariant violation: Maximum update depth exceeded不变量违规错误:超出最大更新深度
【发布时间】:2019-12-28 07:22:57
【问题描述】:

我是 react native 的初学者,我正在尝试通过使用状态和函数来制作一个测验应用程序,以根据给定的单词检查按下的按钮是否是正确的选项。但是,我收到了关于不变量违规的错误:超出了最大更新深度,这可能是由于我使用 onPress 的方式造成的吗?

import React, {Component} from 'react';
import {
  View,
  Text,
  SafeAreaView,
  TouchableOpacity,
  Image,
  ScrollView,
  StyleSheet,
} from 'react-native';
import {color} from 'react-native-reanimated';
import Ionicons from 'react-native-vector-icons/Ionicons';
import {createAppContainer} from 'react-navigation';
import {createBottomTabNavigator} from 'react-navigation-tabs';
import {createStackNavigator} from 'react-navigation-stack';

const styles = StyleSheet.create({
  progressbar: {
    width: 218,
    height: 24,
    borderRadius: 12,
    borderColor: 'grey',
    borderWidth: 1,
    marginTop: 70,
    paddingRight: 79,
    paddingLeft: 79,
  },
  words: {
    fontFamily: 'Arial',
    fontSize: 85,
    fontWeight: 'bold',
  },
  image: {width: 345, height: 391, borderRadius: 50, marginTop: 31},
  button: {width: 100, height: 80, borderRadius: 29, marginHorizontal: 5},
});

export default class quizC extends React.Component {
  state = {
    progress: 0,
    progressbar: 0,
    progressbarwidth: 0,
    words: '',
    imagesource: '',
    option: 0,
    option1: '',
    option2: '',
    option3: '',
    correctoption: 0,
    score: 0,
  };
  _updateProgress() {
    switch (this.state.imagesource) {
      case 1:
        this.setState({words: 'c_t'});
        this.setState({progressbarwidth: 109});
        this.setState({correctoption: 2});
        this.setState({option1: 'c'});
        this.setState({option2: 'a'});
        this.setState({option3: 't'});
        //return cat
        break;

      case 2:
        this.setState({words: 'do_'});
        this.setState({progressbarwidth: 218});
        this.setState({correctoption: 3});
        this.setState({option1: 'd'});
        this.setState({option2: 'o'});
        this.setState({option3: 'g'});
        //return dog
        break;
    }
  }

  _checkcorrectans() {
    if (this.state.option == this.state.correctoption) {
      this.setState(prevState => ({progress: prevState.progress + 1}));
      this.setState(prevState => ({score: prevState.score + 1}));
      this.setState({imagesource: this.state.progress});
    } else {
      this.setState(prevState => ({progress: prevState.progress + 1}));
      this.setState({imagesource: this.state.progress});
    }
  }
  option1() {
    this.setState({option: 1});
    this._checkcorrectans();
    this._updateProgress();
  }

  option2() {
    this.setState({option: 2});
    this._checkcorrectans();
    this._updateProgress();
  }

  option3() {
    this.setState({option: 3});
    this._checkcorrectans();
    this._updateProgress();
  }

  render() {
    return (
      <SafeAreaView>
        <View style={styles.progressbar}>
          <View
            style={{
              height: 24,
              borderRadius: 12,
              backgroundColor: 'green',
              width: this.state.progressbarwidth,
            }}
          />
        </View>
        <View style={{alignItems: 'center', marginTop: 12}}>
          <Text style={styles.words}>{this.state.words}</Text>
        </View>
        <View style={styles.image}>
          <Image src={this._updateProgress()} />
        </View>
        <View style={{paddingLeft: 23, paddingRight: 23, flexDirection: 'row'}}>
          <TouchableOpacity onPress={this.option1()}>
            <View style={styles.button}>
              <Text>{this.state.option1}</Text>
            </View>
          </TouchableOpacity>
          <TouchableOpacity onPress={this.option2()}>
            <View style={styles.button}>
              <Text>{this.state.option2}</Text>
            </View>
          </TouchableOpacity>
          <TouchableOpacity onPress={this.option3()}>
            <View style={styles.button}>
              <Text>{this.state.option3}</Text>
            </View>
          </TouchableOpacity>
        </View>
      </SafeAreaView>
    );
  }
}

【问题讨论】:

    标签: react-native


    【解决方案1】:

    尝试使用

    onPress={() =&gt; this.option1()} 首先是一个匿名函数,然后调用 option1 或 onPress={ this.option1} 。这些将防止重新渲染。

    此错误是由于当您调用 onPress={this.option1()} 时,它调用了函数并导致应用重新渲染,这再次导致函数被调用,从而导致无限循环。最好的方法是不带括号直接调用它onPress={this.option1},或者创建一个匿名函数onPress={() =&gt; this.option1()},它只执行一次

    希望对您有所帮助。如有疑问,请随意

    【讨论】:

      【解决方案2】:

      替换所有onPress方法

      来自:

      onPress={this.option1()}
      

      收件人:

      onPress={() => this.option1()}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-11
        • 1970-01-01
        • 2019-12-03
        • 1970-01-01
        • 2019-07-06
        • 2020-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多