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

Setup for config in persistState #55

Closed Petesta closed 7 years ago

Petesta commented 7 years ago

Hi, I'm struggling to figure out how to select a subset of what config needs to look like with keys/values inside of persistState. There are code snippets but it's a little difficult to piece them together and figure out what is needed to just select a subset of fields to sync to localStorage. Would you be able to elaborate more on how to accomplish this? My state currently looks like below.

{
  key1: {},
  key2: {
    innerKey1: [],
    innerKey2: [],
    innerKey3: null
  }
}

If I wanted to select innerKey1 here, how would I go about that?

// Pseudocode
compose(
  applyMiddleware(thunk),
  persistState('key2', /* config_to_access_innerKey1 */)
);
Petesta commented 7 years ago

In the README.md there is a code snippet for creating a customer slicer. However, I think the code is wrong since in the source when you refer to your slicer with var slicerFn = slicer(paths) the code below...

function myCustomSlicer (paths) {
  return (state) => {
    let subset = {}
    /*Custom logic goes here*/
    return subset
  }
}

Returns an object, not a function which is expected.

screen shot 2016-09-14 at 10 57 53 am

It seems like the code should be something of this sort.

function myCustomSlicer (paths) {
  return (state) => {
    let subset = {}
    /*Custom logic goes here*/
    return () => { subset }
  }
}

Also, state in these functions always happens to be a String and not sure why. Shouldn't it be an object?

lbalceda commented 7 years ago

@elgerlambert could you give an example of how to include only certain inner keys ?

Petesta commented 7 years ago

Here's how it needs to be done.

function customSlicer() {
  return (state) => {
    const { auth } = state;
    return { auth };
  }
}

compose(
  applyMiddleware(thunk),
  persistState(null, { slicer: myCustomSlicer() })
);
lbalceda commented 7 years ago

cool, thanks!

cyberpolin commented 6 years ago

Any one is getting TypeError: Object(...) is not a function