在React性能度量代码中有一个内部错误
There is an internal error in the React performance measurement code
我遇到问题的那部分代码是:
constructor(props){
super(props);
this.state = {
allcars: null,
minValue: 0,
maxValue: 50000,
step: 1000,
firstValue: null,
secondValue: null,
chcboxValue: false,
chcboxManualValue: false,
chcboxAutomaticValue: false
};
this.handleFilterChange = this.handleFilterChange.bind(this);
this.handlePriceUpdating = this.handlePriceUpdating.bind(this);
this.updateCarsToShow = this.updateCarsToShow.bind(this);
this.handleTransmissionUpdating = this.handleTransmissionUpdating.bind(this);
this.resetPriceFilter = this.resetPriceFilter.bind(this);
this.resetTransimissionFilter = this.resetTransimissionFilter.bind(this);
}
componentWillMount(){
this.setState({firstValue: this.state.minValue, secondValue: this.state.maxValue, allcars: this.props.carsToShow});
}
componentWillReceiveProps(nextProps) {
//We get the filter which is removed
let isRemoved = this.props.filters.filter(i => {
return nextProps.filters.filter(a => {
return i.searchFilter[0] !== a.searchFilter[0];
});
});
//If something is removed
if(isRemoved.length > 0){
let removedFilter = isRemoved[0].searchFilter[0]; //The name of removed filter
switch (removedFilter){
case "price":
this.resetPriceFilter();
break;
case "transmission":
this.resetTransimissionFilter();
break;
default:
console.log("Nothing");
}
}
}
resetPriceFilter(){
this.setState({firstValue: this.state.minValue, secondValue: this.state.maxValue, chcboxValue: false});
//We update the cars list in the store
this.updateCarsToShow(this.state.allcars);
}
resetTransimissionFilter(){
this.setState({chcboxManualValue: false, chcboxAutomaticValue: false});
}
如果removedFilter
在componentWillRecieveProps中的值为"transmission",我会得到两个警告:
bundle.js:8335警告:React性能测量代码中存在内部错误。没有期望componentdiduupdate定时器当componentWillReceiveProps计时器仍在进行时启动
bundle.js:71248 Uncaught TypeError: Cannot read property 'top' of undefined
并且如果state
没有更新,如果removedFilter
的值为"transmission"
。如果我在那个情况下控制台记录一些东西,它被显示出来,因此,它在那个情况下,但由于某种原因,状态没有更新。
如果removedFilter
的值为"price"
,则一切正常工作。state
更新了,我没有得到任何警告。
我不确定,但是这个异步行为可能会帮助你。
这里不要使用object来设置状态
this.setState({
firstValue: this.state.minValue,
secondValue: this.state.maxValue,
chcboxValue: false
})
用函数代替
this.setState((prevState, props) => ({
firstValue: prevState.minValue,
secondValue: prevState.maxValue,
chcboxValue: false
}));
所以试试resetTransimissionFilter with
resetTransimissionFilter(){
this.setState((prevState, prop){
chcboxManualValue: false,
chcboxAutomaticValue: false
});
}
因为状态更新可能是异步的
问题可能来自resetPriceFilter
和updateCarsToShow
,您试图在"另一个状态更新"期间更新状态。尝试更改方法,如下所示:
改变:
resetPriceFilter(){
this.setState({firstValue: this.state.minValue, secondValue: this.state.maxValue, chcboxValue: false});
//We update the cars list in the store
this.updateCarsToShow(this.state.allcars);
}
:
resetPriceFilter(){
this.setState({
firstValue: this.state.minValue,
secondValue: this.state.maxValue,
chcboxValue: false
}, () => {
//We update the cars list in the store
this.updateCarsToShow(this.state.allcars);
});
}
这将在之前的状态完成后运行updateCarsToShow
相关文章:
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何在只能有一个asp.net表单的主页上从asp.net页面中的javascript中获取值
- 有可能在来自链接的警报中有一个值吗
- 是否<asp:文本框>有一个onFocusLost事件
- 让谷歌数据图表有一个24小时x轴
- PayPal按钮是否有一个简单的选项,其中金额基于一些用户输入
- 我确实有一个表单,如果用户输入了输入,它应该检查否定或空的输入框,并抛出一条警告消息
- 在这个使用hasOwnProperty的对象扩展程序中有一个错误,I'我不确定那个bug是什么,也不确定这个扩展
- 当知道同一hiearch中至少有一个元素时,遍历到元素.结构使用jquery
- PNG图像被覆盖,但每个图像都有一个链接可以更改,每次一个-Javascript/jQuery/CSS
- JQuery是否有一个“;移动“;作用或者有没有一种更紧凑的方法来做到这一点
- 是否有一个简单的 JQuery 过程来替换页面上每段内部文本的字符串
- 我有一个多边形,我知道所有的4个点.我如何确定一个给定的点是否在多边形内部与OpenLayers
- 确保两个函数一个接一个地执行,其中第一个函数内部有一个异步调用
- 是否有一个API为chrome://web内部/变量在javascript
- 在React性能度量代码中有一个内部错误
- ExtJS 4.1.x-内部有一个组合框的选择器
- 具有点击功能的DIV及其内部的图像有一个死角
- Javascript 自定义函数,内部有一个检查 for 循环
- 摩卡测试之前,每个内部都有一个循环——异步问题