我有这个错误,我以前没有:
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/