简体   繁体   English

将反应组分结合到还原态的一部分

[英]Bind react component to part of redux state

I have redux store that looks something like this: 我有redux商店看起来像这样:

{
  user: {},
  alerts: [],
  reports: [],
  sourses: []
}

For each one of this parts of state i have a bunch of React Components wrapped in a container wich connected via react-redux . 对于这部分状态中的每一部分,我都有一堆React Components包装在一个通过react-redux连接的容器中。 And has mapStateToProps like this 并且像这样有mapStateToProps

(state) => {alerts: state.alerts}
(state, ownProps) => {alert: _.filter(state, {id: ownProps.curId})}

Problem that when i for example launch some action for Alerts like CREATE_ALERT or EDIT_ALERT and redux state updated, ALL REACT COMPONENTS WILL RESPOND TO THIS CHANGE even ones that works with different parts like sources or reports . 问题是,当我例如为CREATE_ALERTEDIT_ALERT等警报启动某些操作并更新redux状态时, 所有反应组件都会响应此更改,即使是那些与sourcesreports等不同部分一起使用的操作也是如此。

My question: how to "bind" certain components to certain parts of a tree. 我的问题:如何将某些组件“绑定”到树的某些部分。 So each container component WILL UPDATE ONLY WHEN APROPRIATE PART OF REDUX STATE UPDATED and ignore other changes. 因此,每个容器组件仅在更新REDUX状态的一部分时更新并忽略其他更改。

Expected behavior 预期的行为

Dispatch CREATE_ALERT -> Alert reducer -> Redux store update -> ONLY Alert container component re-rendering. 调度CREATE_ALERT - >警报减少器 - > Redux存储更新 - > 仅限警报容器组件重新呈现。

When you are changing state in redux the whole state becomes just a new object. 当你在redux中改变状态时,整个状态变成了一个新对象。 Then your component is given by this new object (new reference) and re-renderes itself. 然后你的组件由这个新对象(新引用)给出并重新渲染自己。

To fix this behaviour you need to add some logic to compare if your component got props with different value (not reference). 要修复此行为,您需要添加一些逻辑来比较您的组件是否具有不同值(不是引用)的道具。

The easiest and fastest way is to use React.PureComponent . 最简单快捷的方法是使用React.PureComponent You can also override shouldComponentUpdate function and handle changes by yourself. 您还可以覆盖shouldComponentUpdate函数并shouldComponentUpdate处理更改。 But note that PureComponent works only with primitives (it does a shallow compare). 但请注意, PureComponent仅适用于基元(它进行浅层比较)。

Check also Immutable.js which helps you with intelligent way of changing references of props. 检查Immutable.js ,它可以帮助您更改道具参考的智能方式。

if you use connect method, then pass only selected redux state to the component, this will prevent rendering of other components 如果使用connect方法,则只将选定的redux状态传递给组件,这将阻止呈现其他组件

example: 例:

User Component: 用户组件:

 const mapStateToProps = state =>({
  users: state.users
  });

export default connect(mapStateToProps)(User)

Alert Component: 警报组件:

const mapStateToProps = state =>({
  alerts: state.alerts
  });

export default connect(mapStateToProps)(Alert)

Check this out: Avoid Reconciliation 检查一下: 避免对帐

There explains what Neciu says. 这解释了Neciu说的话。

Container components created with connect will always receive notifications of all updates to the store. 使用connect创建的容器组件将始终接收有关商店所有更新的通知。

The responsibility for consuming these updates falls on the receiving connect component. 使用这些更新的责任在于接收connect组件。 It should contain the logic to extract the data relevant to it. 它应该包含提取与之相关的数据的逻辑。

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

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