0.0.25 • Published 4 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
4 years ago
0.0.24
4 years ago
0.0.23
4 years ago
0.0.22
4 years ago
0.0.20
4 years ago
0.0.21
4 years ago
0.0.19
4 years ago
0.0.18
4 years ago
0.0.17
4 years ago
0.0.15
5 years ago
0.0.16
5 years ago
0.0.14
5 years ago
0.0.13
5 years ago
0.0.12
5 years ago
0.0.11
5 years ago
0.0.10
5 years ago
0.0.9
5 years ago
0.0.8
5 years ago
0.0.7
5 years ago
0.0.6
5 years ago
0.0.5
5 years ago
0.0.4
5 years ago
0.0.3
5 years ago
0.0.2
5 years ago
0.0.1
5 years ago