首页 > 解决方案 > 超过最大更新深度。反应原生

问题描述

我正在尝试将值从一个组件传递到另一个组件。这是我的父组件。

<View>
   { this.state.errors ? <ErrorNotice content={this.state.content} /> : null}
</View>

这是我的子组件

constructor(props){
        super(props);
        this.state = {
            errorMessage : "",
            visibility: false
        }
    }

    closeErrorNotice = ()=>{
        this.setState({
            visibility : false
        })
    }

    render(){
        return (
            <View style={styles.errorContainer}>
                <Text style={styles.errorText}>
                    {this.props.content.errorMessage}
                </Text>
                <Icon
                    name="x"
                    color="#454749"
                    size={25}
                    style={styles.closeButton}
                    onPress= {this.closeErrorNotice()}/>
            </View>
        )
    }

但是我得到了超过最大更新深度。我找不到错误。任何人都可以找到错误。

标签: javascriptreact-native-android

解决方案


传递给时不要调用该函数onPress。你得到一个无限循环,因为你正在调用更新状态的函数,这使得函数再次被调用。您需要传递函数定义,而不是调用函数的结果。

onPress= {this.closeErrorNotice}


推荐阅读