简体   繁体   English

Redux-子组件中的Angular2 / ngrx处理状态

[英]Redux - Angular2/ngrx handling state in child components

there is rule in redux that state should be modified only in reducers. 在redux中有一个规则,即只能在化简器中修改状态。 Does anyone knows best practices for handling state into child components? 有谁知道处理子组件状态的最佳实践? i mean how to react on some event in component which is not "container" should child component be able to modify its internal @intput model or it should only trigger some event which then "container" should transform into action dispatch and model change should return back to this component via @input? 我的意思是,如果子组件能够修改其内部@intput模型,或者应该仅触发某些事件,然后“容器”应转换为动作分派,并且模型更改应返回,则如何对非“容器”组件中的某个事件做出反应通过@input返回到此组件? or child component can modify its model ? 还是子组件可以修改其模型? let say we have datepicker component. 假设我们有datepicker组件。 When selecting date in handler functions should we update this.date or we should only trigger dateChange event and wait for model to being changed by container? 在处理程序函数中选择日期时,我们应该更新this.date还是仅触发dateChange事件并等待容器更改模型? thanks 谢谢

There is good concept of stateful and stateless components in Angular ecosystem. Angular生态系统中有状态和无状态组件的概念很不错。 So it depend what challenges you are trying to achieve: 因此,这取决于您要解决的挑战:

  • does your component can receive initial data from your route 您的组件可以从您的路线接收初始数据吗
  • does components has living knowledge of the current state 组件是否具有当前状态的实时知识
  • is this information valid between other components state changes 此信息在其他组件状态更改之间是否有效
  • how many external dependencies/communications this component has 该组件有多少外部依赖项/通信
  • has stateful/stateless child components, etc 具有有状态/无状态子组件等
  • contains Redux actions (ngrx/store or ng2redux for example) 包含Redux操作(例如ngrx / store或ng2redux)

Please read this article to get more information about differences between this component types. 请阅读本文以获取有关此组件类型之间差异的更多信息。

After that you need to draw your own picture what kind of approach is best suited to your own architecture. 之后,您需要绘制自己的图片,哪种方法最适合您自己的体系结构。

https://toddmotto.com/stateful-stateless-components https://toddmotto.com/stateful-stateless-components

so my example looks like below: 所以我的示例如下所示:

<plumb [nodes]='nodes$|async' [connections]='connections$|async'  (connect)='onConnect($event)' (disconnect)='onDisconnect($event)')/>

basicaly its graph control to draw diagrams.. i am givint this cmoponent as parameters list of blocks/ndoes and list of connection and i am triggering two events when user connects items or disconets in graph.. so the question is if modification of "connections" list should be inside component or when user draw connection component should trigger event and wait with drawing arrow till ngrx store will handle it and give back new list of connections:) i dont wanna this componnet to be awawre of store cause i want this to be reusable.. according to article you sent looks like its better to have components stateles so it shouldnt modify connections list itselfe. 从根本上说,它是通过图形控件来绘制图形的。.我将此菜单项作为块/对象的参数列表和连接列表,并且在用户连接图形中的项目或断开连接时触发两个事件。 “列表应该在组件内部,或者当用户绘制连接组件应该触发事件并等待绘制箭头,直到ngrx存储将处理它并返回新的连接列表:)我不想将此组件唤醒存储,因为我希望这样做可重用..根据您发送的文章,使组件具有状态更好,因此不应修改连接列表本身。 thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM