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)
        })
    }


<DatePicker value={this.state.taskDate as any} locale={locale} format='YYYY-MM-DD' onChange={this.onChangeDate} />

完美解决。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。