-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Array items now have unique, stable keys (#1046) #1335
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you recommend here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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-itemkey
or 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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