We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The below code expects a selector... if no selector is used, an error is thrown.
const state = useSyncExternalStore(store.subscribe, () => selector(store.get()));
This can be fixed be just returning setting the getter to store.get when no selector is passed. Some like below.
const getter = selector ? () => selector(store.get()) : store.get; const state = useSyncExternalStore(store.subscribe, getter);
const getter = selector ? () => selector(store.get()) : store.get;
const state = useSyncExternalStore(store.subscribe, getter);
The text was updated successfully, but these errors were encountered:
I prefer to keep the get and set methods separate, something like this :
get
set
function useStore() { const store = useContext(StoreContext); if (!store) { throw new Error('Store not found'); } function query<SelectorOutput>(selector: (store: Store) => SelectorOutput) { return useSyncExternalStore( store.subscribe, () => selector(store.get()), () => selector(initialState) ); } const dispatch = store.set; return { query, dispatch }; }
So in my other components i can just use get or set
const { query, dispatch } = useStore(); const fieldValue = query((store) => store['value']); // retrieve value dispatch({ value : 'Jon'}); // set value
Sorry, something went wrong.
@mazengh maybe it can help you: const useStoreSetter = () => useStore(() => null).at(1);
const useStoreSetter = () => useStore(() => null).at(1);
Merge pull request #1 from craig-waite/extended-context
7ca2b40
Extended context
No branches or pull requests
The below code expects a selector... if no selector is used, an error is thrown.
const state = useSyncExternalStore(store.subscribe, () => selector(store.get()));
This can be fixed be just returning setting the getter to store.get when no selector is passed. Some like below.
const getter = selector ? () => selector(store.get()) : store.get;
const state = useSyncExternalStore(store.subscribe, getter);
The text was updated successfully, but these errors were encountered: