React antd DatePicker组件的坑(Can't perform a React state update on an unmounted component. This is a...

报错信息:
Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

意思是你的组件已经销毁了,你还设置个锤子的值

然后试着用生命周期函数componentWillUnmount处理,发现没卵用

再去查询资料,原因是你在设置值的时候需要把这个值给包装一下,
因为DatePicker 的value属性不能用string直接赋值,用moment包装一下,包装成moment类型的对象(详情点这里)
也就是这样

// 就是用moment方法,对dateString进行包装即可
    onChangeDate = (date: any, dateString: any) => {
        this.setState({
            taskDate: moment(dateString)
        })
    }



完美解决。

你可能感兴趣的