简体   繁体   English

React,Redux设计目标

[英]React, Redux design desision

The problem: 问题:

Cant individually rerender selectable components without rerendering components that don't have to be rerendered . 不能单独重新渲染可选组件,而不渲染不必渲染的组件。

Some research: 有些研究:

The basic idea of the app: It has two separate item pools, 1: 'input items' 2: 'output items'. 该应用程序的基本思想:它有两个单独的项目池:1:“输入项目” 2:“输出项目”。 Input/s can be assigned to Output/s but not opposite. 可以将输入分配给输出,但不能相反。 Assigning is done by selecting inputs (click on them) and just pressing some of the output items. 通过选择输入(单击输入)并仅按下某些输出项即可完成分配。 But same logic does not make a sense in opposite way. 但是,相同的逻辑没有相反的意义。 So if some outputs are selected and input has been pressed it would be nice to deselect those outputs. 因此,如果选择了某些输出并按下了输入,则取消选择这些输出会很好。

state trasition image < visual version of description below 状态转换图像 <以下描述的可视版本

My first idea was to make SELECT/UNSELECT acton creators and 'selection' reducer what updates selection state based on item triggered actions. 我的第一个想法是让SELECT / UNSELECT acton创建者和“ selection”简化器根据项目触发的动作来更新选择状态。 (Basically, this reducer decides what is selected and what is not) Pool containers listen to this reducer and accordingly updates its child items. (基本上,此化简器决定选择什么,不选什么)池容器侦听此化简器,并相应地更新其子项。

In this scenario, I face a huge problem. 在这种情况下,我面临一个巨大的问题。 Whenever a user clicks the item - every single item is rerendered. 每当用户单击该项目时,都会重新呈现每个项目。 It feels like insane processing overkill just to change a few object properties. 感觉就像疯狂的处理过大的杀伤力,只是为了改变一些对象的属性。 Besides item count can easily reach few hundreds. 此外,项目数可以轻松达到几百个。

A bit better performance solution in my mind was to let the items to toggle appearance themselves and under right circumstances dispatch UNSELECT_INPUTS event from 'selection' reducer. 我认为更好的性能解决方案是让项目自己切换外观,并在适当的情况下从“选择” reduce调度UNSELECT_INPUTS事件。 Create some kind of 'inputsDeselected' reducer and make pools to listen for that. 创建某种类型的“ inputsDeselected” reducer,并创建池来监听。

This scenario doesn't look cool either. 这种情况也不是很酷。

So while stumbling over redux stuff found this piece of code: 因此,在重做redux时,发现了以下代码:

     this.unsubscribe = store.subscribe(() => {
        this.setState({ storeState: store.getState() });
     });

It can be used to dynamically subscribe to 'selection' reducer when item selected and unscribe when item unselected by a user. 当用户选择项目时,它可以用于动态订阅“选择” reduce,当用户未选择项目时,它可以取消订阅。 If the reducer change item state while it is selected it has a chance to respond accordingly. 如果在选择减速器时改变其状态,则有机会做出相应的响应。 This probably could make performance issues acceptable. 这可能会使性能问题可以接受。

So far this is my own research and knowledge state in react-redux. 到目前为止,这是我对react-redux的研究和了解。 Hope that makes any sense. 希望有任何道理。

The questions are: 问题是:

How would you solve this 'problem'? 您将如何解决这个“问题”? Is the last scenario acceptable from the standpoint of Redux? 从Redux的角度来看,最后一种情况是否可以接受?

I would really appreciate your knowledge how to solve this. 我非常感谢您的知识如何解决此问题。

PS I'm pretty new to all this web UI stuff and Javascript ecosystem so confusion is pretty huge looking to all of this. PS我对所有这些Web UI内容和Javascript生态系统还很陌生,因此对所有这些都感到困惑。

I know I could do this by some SelectionState object and few callbacks from selected items or some another 'shortcut' way, and it would be very acceptable for a scale of this project. 我知道我可以通过一些SelectionState对象和从选定项中进行的少量回调或其他“捷径”方式来做到这一点,对于这个项目的规模来说,这是可以接受的。 But I thought it would be cool to make this in more or less Redux 'right' way. 但是我认为以某种或多或少的Redux“正确”方式实现这一点很酷。

Thank you for reading all this crap! 感谢您阅读所有这些废话! :) :)

Well if I got that right 好吧,如果我说对了

Cant individually rerender selectable components without rerendering components that don't have to be rerendered . 不能单独重新渲染可选组件,而不渲染不必渲染的组件。

You should take a look at React PureComponent . 您应该看看React PureComponent It basically shallow compares you component's props on shouldComponentUpdate method and if the props have not changed it doesn't re-render (returns false ). 基本上,它会在shouldComponentUpdate方法上比较组件的道具,如果道具未更改,它不会重新渲染(返回false )。 You can still do it manually in a regular Component if you need more complex comparsions. 如果需要更复杂的比较,仍可以在常规组件中手动进行操作。

This is a great article about how ReactDOM works and it gave me some real good insights on how to increase the performance of my applications. 这是一篇关于ReactDOM的工作原理的很棒的文章,它为我提供了一些有关如何提高应用程序性能的真实见解。

https://evilmartians.com/chronicles/optimizing-react-virtual-dom-explained https://evilmartians.com/chronicles/optimizing-react-virtual-dom-explained

Good luck! 祝好运!

It is hard to say for sure without seeing your code, but I think you might benefit from having a state that contains raw data for your inputs and outputs. 很难确定没有看到您的代码,但是我认为您可以从包含输入和输出原始数据的状态中受益。 Then a separate state that keeps track of which inputs are selected and which outputs are selected, that way only the outputs which have selected state changed will be re rendered. 然后,一个单独的状态会跟踪选择了哪些输入和选择了哪些输出,这样,只有已更改状态的输出才会重新呈现。

For example data state: { inputs: [{ id: 1 }, { id: 2 }], outputs: [{ id: 3 }, { id: 4 }] } 例如数据状态:{输入:[{id:1},{id:2}],输出:[{id:3},{id:4}]}

selected state: { selectedInputs: [1], selectedOutputs: [3] } 选定状态:{selectedInputs:[1],selectedOutputs:[3]}

For clearing out the outputs, you would just clear out the selectedOutputs array. 为了清除输出,您只需要清除selectedOutputs数组即可。

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

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