gpt4 book ai didi

javascript - 将 updater 方法置于状态中还是直接传递到上下文?

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

当有这样的上下文时:

<MatchContext.Provider value={this.state.match}>

然后我应该放

match: {
match: null,
updateMatch: this.updateMatch
},

在我的州或者这样可以吗?

<MatchContext.Provider value={{
match: this.state.match.match,
updateMatch: this.updateMatch
}}>

我在某处读到后者对性能不利,因为它必须在每次渲染或其他操作时重新初始化对象。不记得在哪里读到过。

最佳答案

你的权利,

您应该使用第一种方法并将 updateMethod 存储在组件状态中。

因为如果每次创建一个新对象,状态的内存值都会改变,并且每次组件重新渲染时上下文都会更新

All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes.

https://reactjs.org/docs/context.html#contextprovider

关于javascript - 将 updater 方法置于状态中还是直接传递到上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55000083/

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