Array items now have unique, stable keys (#1046)#1335
Array items now have unique, stable keys (#1046)#1335epicfaace merged 8 commits intorjsf-team:masterfrom
Conversation
|
When using componentWillReceiveProps, the incoming props may be updated via onChange before the setState call completes. Latest change moves onChange to setState callback to ensure it's updated first. This didn't appear to be an issue with getDerivedStateFromProps. |
epicfaace
left a comment
There was a problem hiding this comment.
Looks good, can you add some tests?
src/components/fields/ArrayField.js
Outdated
| /* | ||
| // React 16.3 replaces componentWillReceiveProps with getDerivedStateFromProps | ||
| // | ||
| static getDerivedStateFromProps(nextProps, prevState) { |
There was a problem hiding this comment.
I think it might be cleaner (and easier to migrate to React 16) just to use getDerivedStateFromProps and https://github.com/reactjs/react-lifecycles-compat for now?
There was a problem hiding this comment.
Done in latest commit.
|
@fsteger do you think there is any way to pass on this benefit to those who make their own |
Yeah; that was my intention. Each item in the array of I updated the |
src/components/fields/ArrayField.js
Outdated
| <div key={props.index} className={props.className}> | ||
| <div | ||
| key={props.key} | ||
| id={`array-item-${props.key}`} |
There was a problem hiding this comment.
I don't think there's any point in exposing the key to the user as the id? Currently ids are only used as per the idSchema (for example, root_listOfStrings_0) so having another function for id may also not be the best.
There was a problem hiding this comment.
What would you recommend here?
There was a problem hiding this comment.
Can we just remove the id?
Or is there a particular reason why you thought of adding it?
There was a problem hiding this comment.
I'm using the id right now to ensure that the key does not change inside the ArrayField tests. If you remove the id, the tests for inserting new rows will fail. The re-order and delete tests only pass when the attribute is removed because the id both before and after the action are undefined, but these should really test to ensure an id exists as well.
If we remove the id, I'm not sure how these tests should be updated to verify the correct behavior as the key doesn't appear to be exposed anywhere else.
Regarding the idSchema, were you thinking of just passing the key into the toIdSchema method for each item, and then accessing it via props.idSchema.key?
There was a problem hiding this comment.
Got it. I don't think we should add it to the idSchema or expose it as the id attribute, though, given that the array item ids have no meaning apart from being unique and stable (as opposed to the other ids in the idSchema).
Here are two possible solutions:
- Can we expose the key as
data-rjsf-itemkeyor something instead? - Or, we don't expose the key at all -- in our tests, create a custom ArrayFieldTemplate that exposes the key in the DOM so that the tests can access it.
There was a problem hiding this comment.
I removed the id attribute, and updated the tests to use a custom template.
|
Great, thanks! |
Reasons for making this change
Array items did not have stable, unique keys and instead had to rely on the index of the item.
Fixes #1046
Fixes #1333
Checklist