0.0.5 • Published 1 year ago
@onekstar/store v0.0.5
@onekstar/store
install
yarn add @onekstar/store
pageStore(state of component)
import { usePageStore } from "@onekstar/store";
const state: { count: number } = {
count: 1,
};
const App = () => {
const pageStore = usePageStore(state);
const onBtnClick = () => {
pageStore.setState({ count: pageStore.state.count + 1 });
};
return <button onClick={onBtnClick}>count: {pageStore.state.count}</button>;
};
export default App;
appStore(state between components)
// use initAppStore create an appStore,for example: countStore
import { initAppStore } from "@onekstar/store";
const state: { count: number } = {
count: 1,
};
export const useCountStore = initAppStore(state);
use hooks to consume appStore
// use useCountStore hooks to get or set a global state
import { useCountStore } from "@onekstar/store";
const App = () => {
const countStore = useCountStore();
const onBtnClick = () => {
countStore.setState({ count: countStore.state.count + 1 });
};
return <button onClick={onBtnClick}>count:{countStore.state.count}</button>;
};
export default App;
API
declare type IUsePageStore = <T>(initPageState: T) => {
state: T;
setState: Dispatch<Partial<T>>;
};
export declare const usePageStore: IUsePageStore;
declare type IInitAppStore = <T>(initState: T) => () => {
state: T;
setState: Dispatch<Partial<T>>;
};
export declare const initAppStore: IInitAppStore;