elgerlambert / redux-localstorage

Store enhancer that syncs (a subset) of your Redux store state to localstorage.
MIT License
1.32k stars 107 forks source link

Apply slicerFn in store enhancer to filter paths on store initialization #69

Open giannif opened 7 years ago

giannif commented 7 years ago

I'm using paths to store pieces of my state. The paths I specify don't influence the initial state, they only affect what's stored after the state changes.

I'd expect my store to only to get paths from redux localStorage that I specify when creating the store

On page one, I use path "A"

compose(applyMiddleware(...middleware), persistState(["A"], { slicer }), devTools)

On page two, I use path "B"

compose(applyMiddleware(...middleware), persistState(["B"], { slicer }), devTools)

If I first land on page one, and then go to page two, "A" will be in my state even though it's not in my paths.

When I leave page two, "A" will be removed, and "B" will persist, but I was expecting to not get "A" on page two at all

If the slicer was applied to persistedState in this persistState.js, it'd fix this issue:

const slicerFn = slicer(paths)
persistedState = slicerFn(deserialize(localStorage.getItem(key)))

I could submit a PR if you're merging those :) Or if this was done intentionally, lmk