将更新程序方法置于状态或直接传递给上下文?
Put updater method in state or pass directly to context?
当有这样的上下文时:
<MatchContext.Provider value={this.state.match}>
我是不是应该把
match: {
match: null,
updateMatch: this.updateMatch
},
在我的状态
或者这样可以吗?
<MatchContext.Provider value={{
match: this.state.match.match,
updateMatch: this.updateMatch
}}>
我在某处读到后者对性能不利,因为它必须在每次渲染或其他操作时重新初始化对象。不记得我在哪里读到的。
你的权利,
您应该使用第一种方法并将 updateMethod 存储在组件状态中。
因为如果你每次都创建一个新的对象,state的内存值会改变,context会在每次组件重新渲染时更新
All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes.
当有这样的上下文时:
<MatchContext.Provider value={this.state.match}>
我是不是应该把
match: {
match: null,
updateMatch: this.updateMatch
},
在我的状态 或者这样可以吗?
<MatchContext.Provider value={{
match: this.state.match.match,
updateMatch: this.updateMatch
}}>
我在某处读到后者对性能不利,因为它必须在每次渲染或其他操作时重新初始化对象。不记得我在哪里读到的。
你的权利,
您应该使用第一种方法并将 updateMethod 存储在组件状态中。
因为如果你每次都创建一个新的对象,state的内存值会改变,context会在每次组件重新渲染时更新
All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes.