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

Maintaining component state when reparenting #13

Open
wokalski opened this issue Jan 26, 2019 · 0 comments
Open

Maintaining component state when reparenting #13

wokalski opened this issue Jan 26, 2019 · 0 comments

Comments

@wokalski
Copy link
Member

It is often useful to maintain the state of a component when reparenting or changing the order of elements. Currently you cannot express the former and you express the latter by using keys. The use cases of reparenting are kind of an open question. Replacing key with constant identity stored in state could be an elegant and performant alternative solution for keys. The API would not be cumbersome with Hooks.

In the example below, foo and bar would be passed the same constant identity

let foo = (~contentId as id, children) = component(hooks => {
  <view id />
});

let bar = (~contentId as id, children) = component(hooks => {
  <view id />
});

let baz = () = component(hooks => {
  let (id, hooks) = Hooks.componentId(hooks);
  (hooks, Random.bool() ? <foo contentId=id /> : <bar contentId=id />)
});

An open question is what happens when you render two views with the same component identity at the same time (which is also problematic with keys but you expect keys to break in such cases)

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