React-Native =不变违规:超过最大更新深度

Rom*_*tin 5 react-native

我遇到了这个错误,而我之前没有遇到过: 这是错误的图片 不变违规:超出最大更新深度。当组件重复调用componentWillUpdate或componentDidUpdate内部的setState时,可能会发生这种情况。React限制了嵌套更新的数量,以防止无限循环。

此错误位于:Connect中(位于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>
Run Code Online (Sandbox Code Playgroud)

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

这是我的代码:

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>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Zoh*_*rut 17

尝试:

<TouchableOpacity onPress={() => this.setState({ butPressed: true })}>
Run Code Online (Sandbox Code Playgroud)

代替

<TouchableOpacity onPress={this.setState({ butPressed: true })}>
Run Code Online (Sandbox Code Playgroud)

将{this.setState}分配给不带箭头功能的onPress会导致一遍又一遍的渲染,因为setState将组件重新封装,然后再次进行onPress = {}的分配。使用箭头功能会导致分配功能,因此只有在激活功能后才实际发生setState。(仅当激活onPress时)