React - 冻结部分状态?
我有一个特殊的输入字段,单击后会打开带有复选框选项的下拉菜单。当我输入此输入时,我只需要显示包含键入字符的选项。当我添加字符时它工作得很好,但是当我清空选项数组(存储在状态中)时,我没有得到值。 我尝试使用 Object.freeze 冻结状态(选项),并使用这个“冻结”数组作为起点。但它并没有冻结,我不知道如何做到这一点。 代码
let coppiedOptions = [...state.inputs.names.options];
coppiedInputs.forEach(el => Object.freeze(el));
let newOptions = coppiedOptions.filter(opt => opt.value.includes(action.value));
这是我正在使用Reducer 的 ,因此我获得了作为操作有效负载的值。这个“newOptions”正在被传递到减速器中的状态
I have pecuilar input field which on click opens dropdown with checkboxes options. When I type in this input, I need to show only the options that include typed characters. It works fine as I add characters, but as I empty an array of options (which is stored in state), I don't get values back.
I tried freezing piece of state (options), by using Object.freeze, and use this "frozen" array as starting point. But it doesn'r freeze, and I have no clue how to do this. Here's the code
let coppiedOptions = [...state.inputs.names.options];
coppiedInputs.forEach(el => Object.freeze(el));
let newOptions = coppiedOptions.filter(opt => opt.value.includes(action.value));
I am using use Reducer, so I get value as an action payload. And this "newOptions" is being passed into state in reducer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唯一的解决方案是编写另一个选项数组,具有不同的名称和相同的数据,以复制它并使用它进行过滤,同时仅更改第一个选项数组。我认为仅用一组选项是不可能做到的。我是个白痴
The only solution was writting another options array, with different name, with same data, to coppy it and use it for filtering, while changing only the first one. I quest it is impossible to do with only one set of options. I am an idiot