react项目中报“Cannot update during an existing state transition“问题解决

在react项目中遇到一个这样的报错,根据提示信息我试着将写在render方法中的路由跳转写到了事件函数中,就解决了该问题。大致的意思在跳转的时候不能在render方法中更新props或state,只要避免这么干就不会出现问题,

index.js:1 Warning: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.
<Button onClick={
    
     this.props.history.push('/mapPage') }>去地图页面</Button>

/**********************************将上述代码改为:*****************************************/


render(){
    
    
	return(
		<div>
			<Button onClick={
    
     this.fnToMap) }>去地图页面</Button>
		</div>
	)
}
fnToMap=()=>{
    
    
	this.props.history.push('/mapPage')
}

猜你喜欢

转载自blog.csdn.net/cautionHua/article/details/115717924
今日推荐