[英]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个主要的行动途径:
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.