Skip to content

handling contextual state like auth #129

Answered by bowheart
joprice asked this question in Q&A
Discussion options

You must be logged in to vote

@joprice Cool! A few things:

  • The injectEffect shouldn't be necessary there, unless you specifically want to wait a tick
  • I'd always return the store, not the store's state, from the state factory. Zedux will create another, different store for this atom instance if you don't.
const ignoreEmptyAtom  = (name, target)  =>
    atom(name, () => {
        const value = injectAtomValue(target);
        const current = injectStore(value);
        if (value) {
            current.setState(value);
        }
        if (!current.getState()) {
            throw new Error("Attempted to access atom before initial value was set");
        };
        return current
    });

Replies: 2 comments 11 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
11 replies
@bowheart
Comment options

@joprice
Comment options

@joprice
Comment options

@bowheart
Comment options

Answer selected by joprice
@joprice
Comment options

@bowheart
Comment options

@joprice
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants