为什么我们要在Angular NGRX中定义初始状态,目标是什么

发布于 2025-02-13 17:24:17 字数 95 浏览 0 评论 0原文

我正在为我的应用使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2025-02-20 17:24:17

由于还原器的工作方式,因此初始状态是强制性的。它始终采用当前状态,基于派遣行动将进行更改并返回新状态。

如果不初始状态,当第一个动作被派遣时,还原器将错过“起点”。

是的,您可以将所有状态属性定义为使用的可选属性,然后向还原器提供空的初始状态,但这确实取决于您的用例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文