javascript - React - 复制状态不返回新对象
问题描述
我试图在 React 的组件函数中复制我的状态,以便我可以更改对象以发出 Ajax 请求。
这是我的代码:
fetchNewThumbnails = () => {
const { publicationsFilter, currentQuery } = this.state;
const params = Object.assign({}, currentQuery);
console.log(params)
console.log(currentQuery)
if (publicationsFilter !== 'All') {
params.test = 'test';
}
// some Ajax request here
}
问题是当我params
在 if 语句中编辑复制的对象时。publicationsFilter
当我复制它并使用我的设置第二次运行该函数时,我'All'
希望复制的params
对象只是状态的副本,没有test
对象中的键,但它仍然存在于函数上次运行时。
记录currentQuery
对象总是返回没有params.test
密钥的状态,所以我认为它只会复制currentQuery
对象,而不是保留前一个params
对象。
不完全确定我哪里出错了,如果有任何不清楚的地方,请告诉我!
编辑
currentQuery 对象如下所示:
filter: {
content_type: "thumbnail",
fields.thumbnailType.fields.type[match]: "News & Press",
fields.thumbnailType.sys.contentType.sys.id: "thumbnailType"
}
解决方案
推荐阅读
- gcc - 尝试缓冲区溢出时堆栈上出现意外值
- javascript - 表单将在第一次提交时更新数据库,但在页面刷新之前不会再次更新
- xml - 从 xml 中提取(模式)标签
- java - 如何打印用户占用空格的字符串?
- ios - CurrentValueSubject 和 @Published 之间的区别
- go - 是否可以组合多个 SHA1 状态以在 Golang 中获得最终状态?
- c++ - 为什么左操作数所需的错误左值不会出现在字符串中?
- javascript - 反应 setState(null) 并强制更新
- python - ttk.Combobox - 单击时不显示下拉框
- actionscript-3 - ActionScript 模拟不同的时区