react.js - react componentDidUpdate() prevState !== this.state 问题

查看:284
本文介绍了react.js - react componentDidUpdate() prevState !== this.state 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

componentDidUpdate(prevProps, prevState) {
    console.log(1, prevState.receiveSearch !== this.state.receiveSearch, prevState.receiveSearch, this.state.receiveSearch)
    if (prevState.receiveSearch !== this.state.receiveSearch) {
        this.setState({
            receiveSearch: {}
        });
    }
}

为什么prevState.receiveSearch, this.state.receiveSearch打印出来一样!==却是true

解决方案

this.state.receiveSearch //指向的不是同一个对象, 所以prevState.receiveSearch !== this.state.receiveSearch ==> true 

我猜你在上一步的操作为

this.setState({receiveSearch: {click: false}}) 或
this.setState({receiveSearch: {click: true}})
//其实都是生成了一个新对象, 在比较过程中严格不等于(!==)肯定就是true了.

这篇关于react.js - react componentDidUpdate() prevState !== this.state 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆