简体   繁体   English

单个组件中的多个redux存储

[英]Multiple redux stores in single component

I have been entered into react project which uses multiple redux stores. 我已进入使用多个redux商店的react项目。

There many kind of interfaces: tabs, context, popups and other. 有很多种接口:制表符,上下文,弹出窗口等。 And architect of this project decide to split this store to make this project more faster.I cant change architecture, and have to work as it does. 这个项目的架构师决定拆分这个商店,使这个项目更快。我不能改变架构,必须像它一样工作。

And sometimes I encounter situations, when component needs subcribing to 3 stores. 有时我遇到情况,当组件需要订阅3个商店时。 And It's terrible pain, because first comes from provider, second from props component 这是一个可怕的痛苦,因为首先来自提供者,第二个来自道具组件

  const { someProps } = this.props; return ( <Provider store={SomeStore}> <Route <Component props={someProps} /> </Provider> ) 

and third from State = { store.subsribe } . 第三个来自State = { store.subsribe }

 this.state = Store.getState(); Store.subscribe(() => this.setState(Store.getState())); 

Is it there any possibility to merge multiple stores into one component in a single style? 是否有可能将多个商店合并为一个样式中的一个组件?

you can create some kind of Facade or Proxy - basically HOC that will encapsulate all changes and respond to changes either in props, or in store or elsewhere. 您可以创建某种Facade或代理 - 基本上是HOC,它将封装所有更改并响应道具,商店或其他地方的更改。

I think you can make it similar to Redux store, where main object has properties that represent different data sources/states, that are created by different reducers. 我认为你可以使它类似于Redux商店,其中主对象具有表示不同数据源/状态的属性,这些属性由不同的reducer创建。

Inside your Component you could pass state further down with 在您的Component内部,您可以进一步向下传递状态

  1. props change 道具改变
  2. state provided by Provider update 提供者更新提供的状态
  3. react on reactive data source changes 对反应数据源的变化做出反应

If you work in some isolated area - you can wrap it with this HOC and pass down all data merged from all data sources as prop eg called store . 如果你在一些孤立的区域工作 - 你可以用这个HOC包装它并传递从所有数据源合并的所有数据作为prop,例如称为store

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

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