0.0.25 • Published 5 years ago
nostore v0.0.25
简体中文 | English
nostore
Global state management based on React Hooks
Features
- One API, Simple but efficient
- Strongly typed with Typescript
- React hooks style
- Minimum granularity update component
It can be used as
useState
in the global context. Indeed affect the whole body!
Install
$ yarn add nostore
# or
$ npm install nostore --save
Try It Online
use
create a store
// store.js
import { createStore } from "nostore";
const useStore = createStore({ count: 1 });
export default useStore;
// action
export function useDecrease() {
const [, setStore] = useStore();
return () => {
setStore(prevStore => ({
count: prevStore.count - 1
}));
};
}
// multiple actions
export function useAction() {
const [store, setStore] = useStore();
return {
decrease() {
setStore({
count: store.count - 1
});
},
// async action
async increase() {
await wait(2000);
setStore(prevStore => ({
count: prevStore.count + 1
}));
}
};
}
use store
// Increase.jsx
import useStore, { useAction } from "./store.js";
function Increase() {
const [store] = useStore();
const { increase } = useAction();
return (
<>
<h1>{store.count}</h1>
<button onClick={increase}>increase</button>
</>
);
}
// Decrease.jsx
import useStore, { useDecrease } from "./store.js";
function Decrease() {
const [store] = useStore();
const decrease = useDecrease();
return (
<>
<h1>{store.count}</h1>
<button onClick={decrease} />
</>
);
}
0.0.25
5 years ago
0.0.24
5 years ago
0.0.23
5 years ago
0.0.22
6 years ago
0.0.20
6 years ago
0.0.21
6 years ago
0.0.19
6 years ago
0.0.18
6 years ago
0.0.17
6 years ago
0.0.15
6 years ago
0.0.16
6 years ago
0.0.14
6 years ago
0.0.13
6 years ago
0.0.12
6 years ago
0.0.11
6 years ago
0.0.10
6 years ago
0.0.9
6 years ago
0.0.8
6 years ago
0.0.7
6 years ago
0.0.6
6 years ago
0.0.5
6 years ago
0.0.4
6 years ago
0.0.3
6 years ago
0.0.2
6 years ago
0.0.1
6 years ago