@wener/console
Version:
Base console UI toolkit
37 lines (36 loc) • 1.1 kB
JavaScript
import React, { createContext, useCallback, useContext } from "react";
import { get, set } from "@wener/utils";
import { create as produce } from "mutative";
import { createStore, useStore } from "zustand";
const DefaultContextStore = createStore(()=>{
return {};
});
const Context = /*#__PURE__*/ createContext(undefined);
export const ContextStoreProvider = ({ value, children })=>{
return /*#__PURE__*/ React.createElement(Context.Provider, {
value: value
}, children);
};
function getContextStore() {
return DefaultContextStore;
}
export function useContextStore() {
const store = useContext(Context) ?? DefaultContextStore;
return {
store,
set (path, value) {
store.setState(produce((s)=>{
set(s, path, value, false);
}));
},
get (path) {
return get(store.getState(), path);
},
useWatch (path) {
return useStore(store, useCallback((s)=>get(s, path), [
path
]));
}
};
}
//# sourceMappingURL=useContextStore.js.map