報(bào)錯(cuò)提示如下:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the Timer component.
這里是我的代碼(這里的props是接受另外一個(gè)組建傳過來的數(shù)據(jù)):
class Timer extends PureComponent {
static propTypes = {
aim: PropTypes.number
};
constructor(props) {
super(props);
this.state = {
num: 0,
days: 0
}
}
tick() {
if (this.props.aim) {
const aimTime = new Date(this.props.aim);
const currentTime = Date.parse(new Date());
const leftTime = aimTime - currentTime;
const day = aimTime.getDate();
const hours = aimTime.getHours();
this.setState({
num: hours,
days: day
}, () => {
if (currentTime === this.props.aim) {
clearInterval(this.interval);
}
});
}else{
this.setState({
num: 0,
days: 0
})
}
}
componentDidMount() {
this.interval = setInterval(() => this.tick(), 1000);
}
componentWillMount(){
clearInterval(this.interval);
}
render() {
return (
<span>還剩 {this.state.days} 天 {this.state.num} 小時(shí)</span>
);
}
}
這里附上stackflow里的答案,但是看了下不是很清楚。點(diǎn)擊鏈接
就問題來說,是由于組件 mount 之前就改變了 state,因此這是一個(gè)無效操作。
而 so 上面答案描述的是用與操作符做一個(gè)可行性判斷,也就是確認(rèn)語句在符合條件以后再執(zhí)行。
對于你的代碼,在 componentWillMount 里消除了定時(shí)器,而在 componentDidMount 里又注冊了一個(gè)定時(shí)器,因此這個(gè)定時(shí)器全局存在,那么當(dāng)這個(gè)組件被卸載的時(shí)候,這個(gè)定時(shí)器依然存在,里面的代碼依然運(yùn)行,這時(shí)候 this 指向的就是一個(gè)未渲染的組件了。所以會(huì)有這個(gè) warning。
因此,采用 so 中的解決方案,你可以在 componentWillUnmount 中將這個(gè)定時(shí)器注銷掉,那么組件卸載后這個(gè)定時(shí)器也就不存在了。即不會(huì)產(chǎn)生 warning,也不會(huì)浪費(fèi)性能。
謝邀!
首先恭喜您已經(jīng)解決了!在忙著重構(gòu)react沒來得及看,真是抱歉!我就說說原因吧!
這種錯(cuò)誤一般出現(xiàn)在我們已經(jīng)從DOM中移除了react組件,這時(shí)才調(diào)用setState改變組件的狀態(tài)
。
舉個(gè)比較常見的例子:
我們在componentWillMount中發(fā)送異步請求,并且我們調(diào)用setState根據(jù)響應(yīng)數(shù)據(jù)改變組件的狀態(tài),然后請求還未完成,我們改變了頁面也就是移除該組件,就會(huì)報(bào)這個(gè)錯(cuò)誤。這是因?yàn)殡m然組件已經(jīng)被移除,但是請求還在執(zhí)行,所以會(huì)報(bào)setState() on an unmounted component的錯(cuò)誤
。
我們只要在react組件被移除之前終止setState操作就可以了。
abort()
或 包裹Promise
等 ,請求方式不一樣,處理方法也不同!北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。