Skip to content

[Proposal] atomWithWrapper #1172

Closed Answered by dai-shi
TwistedMinda asked this question in Ideas
May 18, 2022 · 1 comments · 14 replies
Discussion options

You must be logged in to vote

I think it's just a preference, but I would do something like this.

type SetStateAction<Value> = Value | ((prev: Value) => Value)

type State<Value> = {
  useValue: () => Value;
  useSetValue: () => (action: SetStateAction<Value>) => void;
};

const createState = <Value>(initialValue: Value): State<Value> => {
  const a = atom(initialValue);
  return {
    useValue: () => useAtomValue(a),
    useSetValue: () => useSetAtom(a),
  };
};

It doesn't fulfill your constrains though.
btw, I wouldn't overuse atomFamily, because unless you use it with a finite number of params, there's a memory leak risk.

Replies: 1 comment 14 replies

Comment options

You must be logged in to vote
14 replies
@TwistedMinda
Comment options

TwistedMinda May 20, 2022
Collaborator Author

@dai-shi
Comment options

@TwistedMinda
Comment options

TwistedMinda May 20, 2022
Collaborator Author

@dai-shi
Comment options

@TwistedMinda
Comment options

TwistedMinda May 20, 2022
Collaborator Author

Answer selected by TwistedMinda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants