back-store
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

useStore

Minimalist state management tools only for React.js

Features

  • Progressive adoption
  • Minimalist api
  • Extremely reusable
import React from 'react';
import Store, { useStore } from 'back-store';
const Root = () => {
  return (
    <Store value={{ page1: { count: 1 }, page2: { a: { b: 'hello' } } }}>
      <App />
    </Store>
  );
};

const App = () => {
  const [store, setStore] = useStore('page1'); // getting Store state from store
  const { count } = store;
  const plusOne = () => setStore({ count: count + 1 }); // just like setState
  const plusTen = () => setStore(store => ({ count: count + 10 })); // support function
  const minusOne = () => setStore('count', count - 1); // lodash.set like syntax for possible deep structure setStore('a.b.c', xx)
  return (
    <div>
      <div>{count}</div>
      <button onClick={plusOne}>+</button>
      <button onClick={plusOne}>+10</button>
      <button onClick={minusOne}>-</button>
    </div>
  );
};

export default Root;

CHANGELOG

v1.1.0

添加函数式调用

Readme

Keywords

none

Package Sidebar

Install

npm i back-store

Weekly Downloads

0

Version

1.2.0

License

none

Unpacked Size

35 kB

Total Files

20

Last publish

Collaborators

  • fateriddle