为什么我们要在Angular NGRX中定义初始状态,目标是什么
我正在为我的应用使用Angular NGRX,我的问题是为什么我们应该在还原器中定义初始状态,而不是使用“?”。 我的意思是在日期对象中
,您无法再定义初始状态。
I am using angular ngrx for my app, my question is why should we define initial state in reducer instead of using "?" in a date object
I mean that with ?, you can not define initial state any more.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于还原器的工作方式,因此初始状态是强制性的。它始终采用当前状态,基于派遣行动将进行更改并返回新状态。
如果不初始状态,当第一个动作被派遣时,还原器将错过“起点”。
是的,您可以将所有状态属性定义为使用
?
的可选属性,然后向还原器提供空的初始状态,但这确实取决于您的用例。Initial state is mandatory because of how reducer work. It always take the current state and based on dispatched action will do a change and return a new state.
Without initial state the reducer will miss the "starting point" when the first action is dispatched.
Yes, you can define a all state properties as optional with
?
, and then provide an empty initial state to the reducer, but this is really dependent on your use case.