gpt4 book ai didi

reactjs - 在 redux 上更新嵌套对象的更好语法

转载 作者:搜寻专家 更新时间:2023-10-30 21:36:41 26 4
gpt4 key购买 nike

给定如下的 reducer 示例

_({
expandAbility: (state, a: { which: string }) => ({
...state,
report: state.report && {
waitingForIt: false,
content: state.report.content && {
...state.report.content,
interaction: {
expandAbilities: !state.report.content.interaction.expandAbilities.contains(a.which)
? state.report.content.interaction.expandAbilities.add(a.which)
: state.report.content.interaction.expandAbilities.remove(a.which)
}
}
}
}),
})

(下面给出的状态类型仅用于问题上下文目的)

const initialState = {
report: undefined as
| {
waitingForIt?: boolean
content?: {
supportedFightIds: number[]
deathCut: number
reportInfo: any
deathsFull: any
deathsByPlayer: any
deathsByAbility: any
interaction: {
expandAbilities: Set<string>
}
}
}
| undefined,
error: undefined as Error | undefined
}

是否有任何类型的技巧或“当下的 flavor ”库可以让我以更短的方式编写像 expandAbility 这样的 reducer 更新操作? (除了可能创建一些 vars 来引用内部路径)

最佳答案

那里有很多不可变的更新实用程序,请查看 https://github.com/markerikson/redux-ecosystem-links/blob/master/immutable-data.md#immutable-update-utilities 中的一些选项。看看什么最适合你。对于初学者,请查看 Immutability-helper 或 immer。

关于reactjs - 在 redux 上更新嵌套对象的更好语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50360531/

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