This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
Should Roact warn if a key changes type? #88
Labels
status: needs design
Used when we're not sure what the best way forward is
Roact inherits an optimization from React that makes subtrees with the same key reconcile much more quickly. Instead of traversing that children, we just unmount the old tree and replace it with a new one.
The relevant branch of the reconciler:
roact/lib/Reconciler.lua
Lines 297 to 315 in ddac5ae
Problem
This causes some confusing behavior for people who write functions that return components, known as higher-order-components in the React community:
When using makeFoo, the intention is that users would do this:
However, I've found that users will mistakenly do this:
Proposed Solution
I think it would be acceptable for Roact to keep this branch, but throw a warning when it's encountered. This would alert a user if they're doing something that causes Roact to be wasteful.
There are some cases where previously I would've used this feature to implement something like a tab panel, where the same key would be reused when the panel changes. However, I think that embracing dynamic keys in that case, by varying the key based on which panel is being displayed, has other benefits for making such a tab panel more flexible.
What gives me hesitation on this:
The text was updated successfully, but these errors were encountered: