如何刷新组件避免无限循环



我正在从SQLITE数据库中读取数据,在同一组件用户可以在该数据库的同一表中插入数据,因此我想能够插入和显示该数据仅插入的新元素,我的问题是,如果我在组件上调用显示功能将安装不会显示新的插入数据,如果我从组件中调用该显示功能将接收导致无限循环的道具,请您帮助解决此问题 我正在使用redux, props.categes中的数据存储,动作是 displayFunction()

这是我的代码

componentWillMount(){
    let cats = [];
        cats = this.props.categes
        let len = this.props.categes.length;
        this.setState({itemsLength : !this.state.itemsLength})
        I18n.locale = this.props.language
}
componentWillReceiveProps(nextProps) {
   if (nextProps.added) {
    this.props.displayFunction()
    cats = this.props.categes
    let len = this.props.categes.length;
     this.setState({itemsLength : true});
    }
}
componentDidMount(){
    this.setState({showInput : false, showInput2 : false}) 
} 

您是通过在组件生命周期方法中添加setState()方法来创建无限循环。即使组件已卸载,SetState()方法仍会调用。为了防止您参考此链接

还必须检查状态和生命周期方法

最新更新