[英]Warning: Cannot update during an existing state transition (such as within `render` or another component's constructor)
[英]Cannot update during an existing state transition (such as within `render` or another component's constructor)
在下面的代碼中,_updateQID方法是在另一個組件發出事件'Questions.updateHeader'時觸發的。 當發出事件並觸發_updateQID時,我在控制台中收到以下警告:
警告:forceUpdate(...):在現有狀態轉換期間(例如在
render
或其他組件的構造函數中)無法更新。 渲染方法應該純粹是道具和狀態的函數; 構造函數的副作用是反模式,但是可以將其移至componentWillMount
。
import React, { Component } from 'react'
import { subscribe, model } from '~/lib'
import { ContextMenu } from 'project-components'
import _ from 'lodash'
import EventEmitter from 'eventemitter3'
import './Headers.styl'
class Headers extends React.Component {
constructor (props) {
super(props)
this.ee = new EventEmitter()
this._updateQID = this._updateQID.bind(this)
}
componentDidMount () {
model.on('Questions.updateHeader', this._updateQID.bind(this))
}
componentWillUnmount () {
model.removeListener('Questions.updateHeader', this._updateQID.bind(this))
}
_updateQID = () =>{
this.forceUpdate()
}
render () {
return (<div className="header">DUMMY TEST FOR HEADER</div>)
}
}
export default Headers
請幫忙
好的,我們解決了它……我們觸發了另一個組件的render方法中的forceupdate Headers組件的事件。……將其放入componentDidUpdate而不是render之后,警告消失了……
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.