Javascript 异步按钮处理程序函数中的setState值在渲染中不为空
我正在尝试设置asyn buttonHander函数中的值。它在onSortedViewHandler函数中工作正常,但当我尝试在render array中console.log数组值为空时,我不知道为什么,但它没有渲染Javascript 异步按钮处理程序函数中的setState值在渲染中不为空,javascript,reactjs,Javascript,Reactjs,我正在尝试设置asyn buttonHander函数中的值。它在onSortedViewHandler函数中工作正常,但当我尝试在render array中console.log数组值为空时,我不知道为什么,但它没有渲染 state={inputText:,sortedArrayList:[]} onSortedViewHandler = async () => { const data = await this.props.sortedContract.methods
state={inputText:,sortedArrayList:[]}代码>
onSortedViewHandler = async () => {
const data = await this.props.sortedContract.methods
.sortNumberArrayList()
.call()
.then((array) => {});
this.setState({ sortNumberArrayList: data }); // array output is correct here
console.log(data);
console.log(this.state.sortNumberArrayList); // array output is correct here
};
它在这个功能中运行良好
但不是数组在渲染中显示为空
render() {
console.log(this.state.sortedArrayList); //but empty here
}
``
这将使您得到正确的答案。您有不同的命名状态。状态属性有不同的名称:sortNumberArrayList
和SortDarrayList
哦,我的错,非常感谢@DanielCheung
render() {
console.log(this.state.sortNumberArrayList);
}