Skip to content

Derived overwritable atom, that re-derives when source changes #1914

Answered by dai-shi
Lesik asked this question in Q&A
Discussion options

You must be logged in to vote

Hm, I think you can keep "what you are overriding".

const sourceAtom = atom('hi');
const overriddenAtom = atom(undefined);
const mergedAtom = atom(
  (get) => {
    const source = get(sourceAtom);
    const overridden = get(overriddenAtom);
    if (overridden?.source === source) {
      return overridden.override;
    }
    return source;
  },
  (get, set, update) => {
    set(overriddenAtom, { source: get(sourceAtom), override: update });
  }
);

In this case, we don't need timestamps.

Replies: 5 comments 7 replies

Comment options

You must be logged in to vote
2 replies
@Lesik
Comment options

@dai-shi
Comment options

Comment options

You must be logged in to vote
5 replies
@dai-shi
Comment options

@Lesik
Comment options

@dai-shi
Comment options

Answer selected by Lesik
@Lesik
Comment options

@dai-shi
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants