How to modify the component state, if the initial state is empty?
本问题已经有最佳答案,请猛点这里访问。
问题是否可以在初始状态为空的情况下动态添加/修改状态。
描述:实际上这个 tempObj 数据作为对象数组来自后端,然后我修改数据并将其分配给 tempObj,然后我想将其设置为空状态。这样我就可以使用和修改应用程序中的数据。
我做了什么:
我尝试了以下代码,但是当我对其进行控制台时,它显示为空状态。
如果有人能帮我解决这个问题,我将不胜感激,谢谢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | constructor(props) { super(props); this.state = {}; } componentDidMount() { //actually this tempObj data is coming from backend //after few loops i am assigning values like this let tempObj = { 132284688949:{3: false, 5: true, 6: false}, 262993790438:{3: false, 8: true, 11: false}, 262993790440:{3: false, 8: true, 11: false}}; this.setState(tempObj); console.log(this.state); } |
您的信息就在那里。
的回调
1 2 3 | this.setState(tempObj, () => { console.log(this.state); }); |