简体   繁体   English

setState(...):只能更新已安装或安装的组件。 这通常意味着您在已卸载的组件上调用了setState()。 这是一个无操作

[英]setState(…): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op

Can only update a mounted or mounting component. 只能更新已安装或安装的组件。 This usually means you called setState, replaceState, or forceUpdate on an unmounted component. 这通常意味着您在已卸载的组件上调用了setState,replaceState或forceUpdate。 This is a no-op. 这是一个无操作。

Please check the code for the Menu component. 请检查Menu组件的代码。

I don't know what happen. 我不知道发生了什么。 I just keep find this error in my project. 我只是在我的项目中发现这个错误。 I don't really understand about this error because this error doesn't give me specific information. 我真的不明白这个错误,因为这个错误没有给我具体的信息。 Now, i confused this error made me little bit stressed 现在,我很困惑这个错误让我有点紧张

export default class Menu extends Component {
    constructor(){
        super();
        this.state = {
            user: {},
            loading: true,
            dataSource: new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2 }),
            items: [],
        }
    }
    componentDidMount(){
        AsyncStorage.getItem('userData').then((user) => {
            let userData = JSON.parse(user)
            this.setState({
                user: userData,
            })
        })
        firebase.database().ref('Posts').limitToLast(10).on('value', (snap) => {
            console.log('new', snap.val())
            snap.forEach((child) => {
                this.state.items.push({
                    title: child.val().title,
                    key: child.key,
                    author: child.val().authorName,
                    image: child.val().picture,
                    price: child.val().price,
                    description: child.val().description,
                    stock: child.val().stock,
                    authorId: child.val().authorId
                });
            });
            this.setState({ 
                dataSource: this.state.dataSource.cloneWithRows(this.state.items),
                loading:false
            });
        });
    }

    logOut(){
        AsyncStorage.removeItem('userData').then(() => {
            firebase.auth().signOut();
                this.props.navigation.navigate('Index')
        })
    }

    renderRow(data) {
        return (
        <View style={styles.listProduct} key={data.key}>
            <TouchableOpacity 
             onPress={() => this.props.navigation.navigate('Detail', {data})}
            >
            <Text style={styles.titleProduct}> {data.title} </Text>
            <Text style={styles.textProduct}>{data.author}</Text>
            <Image
             source={{ uri:data.image }}
             style={{
                height: 150,
                width: 150,
                alignSelf: 'center',
            }}
          />
        </TouchableOpacity>
        <View style={styles.postInfo}>
          <Text style={styles.textProduct}>Rp.{data.price}</Text>
        </View>
        </View>
        )
    }

    render(){
        if (this.state.loading){
            return <ActivityIndicator size="large" />
        }

        console.ignoredYellowBox = ['Remote debugger'];
        console.ignoredYellowBox = ['Setting a timer'];
        console.log(this.state.user)
        const { navigate } = this.props.navigation
        return(
            <ScrollView>
            <View style={styles.container}>
                <View style={styles.header}>
                    <TouchableOpacity onPress={()=> this.props.navigation.navigate('Checkout')}>
                    <Icon name='md-cart' size={30} color='#eee'/>
                    </TouchableOpacity>
                    <View>
                    <TouchableOpacity onPress={this.logOut.bind(this)}>                    
                    <Icon name='ios-log-out-outline' size={30} color='#eee' />
                    </TouchableOpacity>
                    </View>

                    <View style={styles.addItem}>
                    <TouchableOpacity onPress={() => this.props.navigation.navigate('Add')}>
                    <Icon name='md-add' size={30} color='#eee' />
                    </TouchableOpacity>
                    </View>               
                </View>

                    <ListView
                      dataSource={this.state.dataSource}
                      renderRow={this.renderRow.bind(this)}
                      enableEmptySections={true}
                      />
            </View>
            </ScrollView>
        )
    }
}

You need to remove event listeners any other type of listeners when you un-mount the component. 卸载组件时,需要删除任何其他类型的侦听器的事件侦听器。 Firebase query is trying to update the state event though the component is unmounted. Firebase查询尝试在卸载组件时更新状态事件。 You can use off method to remove listeners of firebase. 您可以使用off方法删除firebase的侦听器。

Another possible problem with your code is that you are manipulating state values directly. 您的代码的另一个可能问题是您正在直接操作状态值。 This is not a good practice. 这不是一个好习惯。 Below code is an example of how you can achieve similar affect with what you are already trying to do. 下面的代码是一个示例,说明如何使用您正在尝试的方式实现类似的效果。

Example

onData = (snap) => {
  console.log('new', snap.val())
  snap.forEach((child) => {
    this.setState((prevState) => {
      const newState = Object.assign({}, prevState);
      newState.items.push({
        title: child.val().title,
        key: child.key,
        author: child.val().authorName,
        image: child.val().picture,
        price: child.val().price,
        description: child.val().description,
        stock: child.val().stock,
        authorId: child.val().authorId
      });
      return newState;
    });
  });
  this.setState({ 
    dataSource: this.state.dataSource.cloneWithRows(this.state.items),
    loading:false
  });
}

componentDidMount(){
    AsyncStorage.getItem('userData').then((user) => {
        let userData = JSON.parse(user)
        this.setState({
            user: userData,
        })
    })
    firebase.database().ref('Posts').limitToLast(10).on('value', this.onData );
}

componentWillUnmount() {
  firebase.database().ref('Posts').limitToLast(10).off('value', this.onData );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只能更新已安装或安装的组件。 这通常意味着您在已卸载的组件上调用了setState() - Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component 警告:setState(…):只能更新已安装或正在安装的组件。 这通常意味着 - Warning: setState(…): Can only update a mounted or mounting component. This usually means 无法在已卸载的组件上调用setState(或forceUpdate)。 这是空操作,但表示您的应用程序内存泄漏 - Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application 只能更新已安装或正在安装的组件。 - Can only update a mounted or mounting component. 尝试在其他类中重用组件并获取错误:“警告:setState(...):只能更新已安装或安装的组件。 ” - Trying to re-use component in other class and getting error: “Warning: setState(…): Can only update a mounted or mounting > component. ” setState(…):这通常意味着您在未安装的组件上调用了set state() - setState(…): This usually means you called set state() on an unmounted component setState只能更新已安装或安装的组件 - setState Can only update a mounted or mounting component setState(…):只能更新已安装或正在安装的组件 - setState(…): Can only update a mounted or mounting component 无法对未安装的组件执行反应 state 更新。 这是一个空操作,但它表示 memory 泄漏 - can't perform a react state update on an unmounted component. this is a no-op but it indicates a memory leak 只能更新已安装或正在安装的组件。 找不到错误 - Can only update a mounted or mounting component. findless error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM