[英]correct time to update array in state react-redux
I just started using react-redux in react applications and only project i made with react-redux was a simple json fetcher and updater, (and that was also based on crash course LOL) and now i am creating second simple application even thought it does not need redux and将与反应完美配合。 所以这是单词/数字赤道。 用户填写第一个和第二个输入,应用程序确定它们是否相等,然后将方程添加到方程的历史(以前的方程)。 所以我的问题是我不知道什么时候应该在减速器或生命周期方法( componentDidUpdate
)中更新previous equations
数组,在他更新生命周期方法的速成课程中,但我读到它也可以在减速器中。 所以哪个是更好的做法
这个:
export default function equateReducer(state = initialState, action) => {
switch(action.type){
case ADD_TO_EQUATIONS:
return {
...state,
equations: [...state, action.payload.equation]
}
}
}
或这个
componentDidUpdate(nextProps){
if(nextProps.equation){
this.props.equations.unshift(nextProps.equation)
}
}
PS:我真的不知道生命周期方法中的更新是如何工作的(我的意思是它是如何获取 nextProps.equation 的)
(之一)Redux 背后的想法只是通过定义的动作改变 state
所以不是直接.unshift over equations 数组(我猜你是从 mapDispatchToProps 连接的),你应该调度一个动作来达到同样的结果(所以你的答案是:第一个选项看起来更好)
如果您认为 redux 对于该任务来说太过分了,请查看 useReducer 钩子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.