简体   繁体   English

Redux的商店外面的Redux州

[英]Redux state outside redux's store

I've a situation where my react-native application has some images that I want to show, 我的情况是我的react-native应用程序有一些我想要显示的图像,

export const myImages = Map()
  .set('image-one, {
    name: 'Foo',
    source: require('./foo'),
  })
  .set('image-two, {
    name: 'Bar',
    source: require('./bar'),
  })

I let my user to select the kind of image in question, like so: 我让我的用户选择有问题的图像类型,如下所示:

export const selectImageSaga = function*({ imageId }) {
  yield put({ type: 'SELECT_IMAGE', imageId /* image-one, image-two, etc. */})

Now, in my store, I have a reference to the image that I want to show. 现在,在我的商店中,我引用了我想要显示的图像。 I could call myImages.get(reduxState.imageId) to get the data that I want, all good and great. 我可以调用myImages.get(reduxState.imageId)来获取我想要的数据,一切都很好。 I really hate this. 我真讨厌这个。

Now both my saga and my react components need to refer to an external and (potentially, I'm not showing this in this example) mutable object to fetch the data they need when they need it. 现在我的saga和我的react组件都需要引用一个外部的(可能,我在这个例子中没有显示这个)可变对象来获取他们需要的数据。

If it wasn't for source , I would simply store the path or uri of the file I'm interested, and that's it, I would keep all my state in redux. 如果它不是source ,我只会存储我感兴趣的文件的路径或uri,就是这样,我会将所有状态保存在redux中。

What are my alternatives here? 这里有什么替代方案?

There's no magic here, I see 3 main avenues of action: 这里没有魔力,我看到了3个主要的行动途径:

  1. Your action sends over all the necessary information. 您的操作会发送所有必要信息。 That can be done by moving the responsibility to the caller of the action, or the action creator itself will fetch the data from the external potentially mutable object, normalize it, and pass it to the reducer. 这可以通过将责任移动到操作的调用者来完成,或者操作创建者本身将从外部可能可变对象获取数据,将其标准化并将其传递给reducer。
  2. Your reducer gets the ID from the action, and combines it with the external object. 您的reducer从操作中获取ID,并将其与外部对象组合。 Again, this means that the redux store has all of the necessary information. 同样,这意味着redux商店拥有所有必要的信息。
  3. Your selector (or if you don't have selectors, your react component) accepts the ID and combines it with your external object. 您的选择器(或者如果您没有选择器,您的反应组件)接受ID并将其与您的外部对象组合。 This works well if you do things like persist your store into localStorage (because then you only persist the ID, not all of the information) 如果您将商店保存到localStorage(因为那样您只保留ID,而不是所有信息),这样做效果很好

The advantage of 1 is that your reducer is stupid and stateless, the logic of fetching the relevant info is done in the action creator. 1的优点是你的reducer是愚蠢和无状态的,获取相关信息的逻辑是在动作创建者中完成的。

The advantage of 2 is that... well, I honestly can't think of too many, let me know if you find one. 2的优点是......好吧,老实说,我想不起太多,如果你找到一个,请告诉我。

The advantage of 3 is that your store remains relatively small and simple, which helps with things like server-side rendering and localStorage persistence. 3的优点是您的商店仍然相对较小且简单,这有助于服务器端呈现和localStorage持久性之类的事情。 But in turn, it makes your React components less pure. 但反过来,它会使你的React组件变得不那么纯净。


There's no silver bullet here I'm afraid, you will need to make the decision based on your project's needs and conventions. 这里没有灵丹妙药,我担心,您需要根据项目的需求和惯例做出决定。

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

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