SetState在我手动启动OBJ而不是传递OBJ时起作用
我正在尝试重复使用不同组件中的对象,所以我的最初OBJ。是空的,但是当我将其导出到我的组件和setState中时,它没有清除状态。但是当我安慰时。知道为什么会发生这种情况吗?
this.state = {
name: '',
lastName: '',
age: 0,
kids: 0
}
const myEmptyObj = {
name: '',
lastName: '',
age: 0
}
// lets pretend this function has name, lastName, age, and kids with values
// this function should clear the state but update the kids to value 3. so everything
// empty but the kids field
myFunctionClearState() {
myEmptyObj.kids = 3;
setState(myEmptyObj);
}
I am trying to reuse an object in different components so my initial obj. is empty but when i export it into my component and setState to obj., it doesn't clear the state. but when I console.log the obj., it comes back as expected. any idea why this is happening?
this.state = {
name: '',
lastName: '',
age: 0,
kids: 0
}
const myEmptyObj = {
name: '',
lastName: '',
age: 0
}
// lets pretend this function has name, lastName, age, and kids with values
// this function should clear the state but update the kids to value 3. so everything
// empty but the kids field
myFunctionClearState() {
myEmptyObj.kids = 3;
setState(myEmptyObj);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论