React-Native = 不变违规 : Maximum update depth exceeded

标签 react-native

我有这个错误,我以前没有:
here is the image of the error
不变违规:超过最大更新深度。当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,就会发生这种情况。 React 限制嵌套更新的数量以防止无限循环。

此错误位于:
在连接中(在 LoginForm.js:75)

render() {
const { inputStyle, containerStylePass, containerStyleIdent, barStyle, textInputStyle } = styles;

return (
   <View>
    <View>{/* all the password form*/}
      <View style={containerStylePass}>
      icon
        <Text style={inputStyle}>Mot de passe</Text>
      </View>
      <TextInput
        secureTextEntry
        autoCorrect={false}
        style={textInputStyle}
      />
      <View style={barStyle} />
    </View>

    <View>
      <Connect />
    </View>
  </View>

我不知道为什么会出错,有人可以帮忙吗?

这是我的代码:
import React, { Component } from 'react';
import { Text, TouchableOpacity } from 'react-native';
import LinearGradient from 'react-native-linear-gradient';

class Connect extends Component {

  render() {
    return (
      <TouchableOpacity onPress={this.setState({ butPressed: true })}>
        <LinearGradient
          colors={['#56EDFF', '#42F4A0']}
          start={{ x: 0.0, y: 1.0 }} end={{ x: 1.0, y: 1.0 }}
        >
          <Text style={textStyle}>
            Se connecter
          </Text>;
        </LinearGradient>
      </TouchableOpacity>
    );
  }
}

最佳答案

尝试:

<TouchableOpacity onPress={() => this.setState({ butPressed: true })}>

代替
<TouchableOpacity onPress={this.setState({ butPressed: true })}>

将 {this.setState} 分配给没有箭头函数的 onPress 会导致一遍又一遍地渲染,因为 setState 将组件重新渲染,然后再次分配给 onPress = {}。
使用箭头函数而不是导致分配一个函数,因此 setState 实际上在该函数被激活之前不会发生。 (仅当 onPress 被激活时)

关于React-Native = 不变违规 : Maximum update depth exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445512/

相关文章:

javascript - React Native 结束游戏屏幕不显示并崩溃

react-native - 提供所需应用程序包的有效路径。打印 : Entry, ":CFBundleIdentifier",不存在

javascript - 如何从另一个组件更改组件状态?

node.js - Macbook React Native 安装

ios - 运行react-native run-ios就成功了。但不启动模拟器

javascript - react native : Can you conditionally render JSX based on the existence of a string?

reactjs - 如何在发送 Content-Type multipart/form-data 的同时将图像/jpeg 文件上传到 s3 上的预签名 url?

react-native - 用嵌套在 Text 中的 TouchableOpacity 包装文本

javascript - 编译时设置错误 react native "gradle"

javascript - React-Native:for 循环中的 this.something.map 可以访问 .json 文件中数据数组中的数据..有人可以建议一个示例代码吗?