1.0.2 • Published 5 years ago
@muzikanto/observable-global v1.0.2
Observable
Introduction
- create global store
- async append store part
- listen store changes
- listen store object part change
- create event and subscribe store to event
- and more..
Installation
npm i @muzikanto/observable @muzikanto/observable-global
# or
yarn add @muzikanto/observable @muzikanto/observable-global
Example
example create store
export interface GlobalState {
userName: string | null;
}
const makeStore = (initialState: Partial<GlobalState>) => {
return combineAsync({
userName: createStore(initialState.userName || ''),
});
};
const store = getOrCreateStore(makeStore, { userName: 'Maxim' });
example async inject
// ...
const store = getOrCreateStore(makeStore, { userName: 'Maxim' });
const asyncStore = createStore(1);
store.injectStore('asyncStore', asyncStore);
example connect
function Component() {
const userName = useGlobal((globalState) => globalState.userName);
return <span>{userName}</span>;
}