gpt4 book ai didi

immutable.js - 如何在 Immutable.js Record 中一次设置多个字段?

转载 作者:行者123 更新时间:2023-12-03 05:51:50 28 4
gpt4 key购买 nike

查看this ,我认为一成不变。 Record是表示“javascript不可变对象(immutable对象)”的数据结构,但我想一次更新多个字段,而不是每次创建多个调用set的对象。

我想做这样的事情

class LoginContext extends Immutable.Record(
{ logged : false, loading: false, error: false, user: null}){
}

var loginContext = new LoginContext()

var anotherContext = loginContext.set({'logged':'true', 'error':'false'})

read您无法将对象传递给 API 的 Record.set()一致性:

Consistency with other uses of set both in this library and in ES6. Map and Set's set can't accept an object, because their keys can be anything, not just strings. Records must have strings, but keeping the API consistent was important.

我知道我可以使用:

var anotherContext = loginContext.withMutations(function (record) {  
record.set('logged','true').set('error','true');
});

还有其他方法还是我误用了 Record?

最佳答案

我个人更喜欢合并语法,感觉更自然:

const newContent = oldContext.merge({
"logged": true,
"error": false
});

如果你正在做一些非常复杂的事情,也许 transient 版本会更好,但我只是无法想象在哪里。

这也意味着您可以根据需要利用 mergeDeep

关于immutable.js - 如何在 Immutable.js Record 中一次设置多个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31664994/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com