Javascript 调用setState时未定义“反应本机状态”字段
当我尝试在我的状态上调用setState时,它的一部分保持不变,因为它在setState调用中未定义 我国:Javascript 调用setState时未定义“反应本机状态”字段,javascript,reactjs,react-native,state,Javascript,Reactjs,React Native,State,当我尝试在我的状态上调用setState时,它的一部分保持不变,因为它在setState调用中未定义 我国: state = { result: '', inputValue: '' } 我称之为setState的部分: const result = this.state.result; let inputValue = this.state.inputValue; inputValue = ''; if (result !== '') { debugger
state = {
result: '',
inputValue: ''
}
我称之为setState的部分:
const result = this.state.result;
let inputValue = this.state.inputValue;
inputValue = '';
if (result !== '') {
debugger
this.setState({ result: '0', inputValue: inputValue }, () =>
console.log(this.state.inputValue + ' result: ' + this.state.result)
)
}
而console.log给了我未更改的值。奇怪的是,当我在其他部分调用setState时,它正在工作并更新状态字段。只有在这个调用中保持不变。您没有在
构造函数中调用它,是吗?您能包含整个组件吗?很难判断是否有错误,因为inputValue
最初是一个空字符串,并且您在setState
No中将其设置为空字符串,我在某个匿名函数中调用它。@Thole我有一种计算器,我对inputValue所做的一切就是用setState将其展开为这样:inputValue='1+2*3',当我尝试在setState中调用上述代码时,inputValue保持不变。您的代码似乎是正确的。我认为问题在于,您没有为结果
分配任何值,因此,如果您查看这里的语句,它将不会执行