I've a situation where my react-native application has some images that I want to show,
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. 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.
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.
What are my alternatives here?
There's no magic here, I see 3 main avenues of action:
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.
The advantage of 2 is that... well, I honestly can't think of too many, let me know if you find one.
The advantage of 3 is that your store remains relatively small and simple, which helps with things like server-side rendering and localStorage persistence. But in turn, it makes your React components less pure.
There's no silver bullet here I'm afraid, you will need to make the decision based on your project's needs and conventions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.