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

報(bào)錯(cuò)信息:
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.

意思是你的組件已經(jīng)銷毀了,你還設(shè)置個(gè)錘子的值

然后試著用生命周期函數(shù)componentWillUnmount處理,發(fā)現(xiàn)沒卵用

再去查詢資料,原因是你在設(shè)置值的時(shí)候需要把這個(gè)值給包裝一下,
因?yàn)镈atePicker 的value屬性不能用string直接賦值,用moment包裝一下,包裝成moment類型的對(duì)象(詳情點(diǎn)這里)
也就是這樣

// 就是用moment方法,對(duì)dateString進(jìn)行包裝即可
    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} />

完美解決。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。