-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
RFC: Open questions around "Collection Shorthand" #604
Comments
ChildKey
I think we should agree that we'll never use
👍 There are other options like generated ids or WeakMap, but I'm afraid these are frail themselves and require too much tooling than is worth it. It seems we should just do as React has done and require the key if anything. Interacting with child props
We search/filter on text/value of the If I'm thinking correctly here, we could update I think we can and should avoid adding the items to state though. If we find that we're creating the Note, |
I believe this is solved in #645. There is now a factory for collection shorthand. It implements a key generator that generates keys based on the final props of the factory. It defers to the key prop first, the childKey prop second (value or function), else it generates a key. If generation fails to produce a unique key, then we leave it to React warnings to inform the user that need to provide unique keys. |
See https://github.com/TechnologyAdvice/stardust/pull/567#discussion_r81444584 for a more detailed description of what I'm calling "Collection Shorthand".
I've been thinking about collection shorthand a little bit and have some open questions that I wanted to bring up so we can start figuring it out.
ChildKey
When rendering the
items
it's necessary to generate a unique key for each item unless one is passed. In some cases it may be possible to do this given the props (see the rendering of eachMenuItem
here. However, there are issues even with this type of approachMenuItem
case the content/name props would be blank).TableRow > TableCell
comes to mind. Imagine a row where each cell is the string "n/a" - we'd be unable to generate a key just from the props.Should we do any of the following:
key
orchildKey
was passed so we're falling back to index-based keys, which can yield unreliable results. You should specify keys yourself to guarantee uniqueness".Warning: Each child in an array or iterator should have a unique "key" prop
.When you're missing keys, React needs to re-render the whole collection because it can't be sure. When you generate duplicate keys for different elements React squashes the duplicates and only renders one. So getting it wrong is much worse than just not doing anything.
Interacting with child props
In some components, it's necessary to be able to use the props for each item created by the shorthand. For example, there's an open issue to allow specifying string elements for
Dropdown
options (#470). In this case, I think it makes sense to make to use shorthand. However, we need to do things like filtering/searching, which rely on turning a literal into a props object or being able to use the props of a passed React element.I'm thinking we can do something like this:
items
have changed we run something like (e.g. forDropdown
):item.props.value
) instead of needing to do something like_.isObject(item) ? item.value : item
.There may be some other issues so feel free to add them here for discussion.
Thoughts?
The text was updated successfully, but these errors were encountered: