1.0.1 • Published 5 years ago

@muzikanto/observable-persist v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Observable-persist

npm version downloads dependencies Status size

Introduction

  • based on observable
  • save state to localStorage and get state on initialize page

Installation

npm i @muzikanto/observable @muzikanto/observable-persist
# or
yarn add @muzikanto/observable @muzikanto/observable-persist

Example

save and get from localStorage

const store = persistStore<number>(1, 'localStorage-key', strategyLS);
const inc = createEvent();

store.on(inc, (s) => s + 1);

function Example() {
    const state = useStore(store);

    return (
        <div>
            state: {state}
            <button onClick={() => inc()}>inc</button>
        </div>
    );
}

Advanced with transform state

const store = persistStore<number>(
    1, 
    'localStorage-key',
     strategyLS,
    [
        // transform deprecated state to normal
        {id: 'to-array', func: (value: number) => [value]},
    ],
);

License

MIT

1.0.1

5 years ago

1.0.0

5 years ago