Skip to content
New issue

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

Adopt variant underlying state management #91

Open
otakustay opened this issue May 19, 2021 · 0 comments
Open

Adopt variant underlying state management #91

otakustay opened this issue May 19, 2021 · 0 comments

Comments

@otakustay
Copy link
Member

otakustay commented May 19, 2021

Pick useArray as an example, this hook creates a bunch of convenient functions around Array types, it manages a local state by using useState in its implementation:

const [list, {push, removeAt}] = useArray();

<CreateForm onSubmit={push} />
<DeleteButton onClick={index => removeAt(index)} />

In real word, we often create states from variant sources, like recoil's useRecoilState, or a abstract [state, setState] passed via Context, then we are not able to create array functions around these states, this is an important missing functionality of a well designed hooks library.

For this reason, we're preparing to split some hooks into 2 parts:

  1. a useSomethingWith([state, setState]) to wrap an existing state tuple
  2. a useSomething hook depending on the previous useSomethingWith with a local state

After this, we can simple introduce useArrayWith to help us manage global arrays like:

const globalListState = useRecoilState(globalListAtom);
const [globalList, {push, removeAt}] = useArrayWith(globalListState);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant