-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[TreeView] Do not mutate props.items
in the getItemTree
method
#19483
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
[TreeView] Do not mutate props.items
in the getItemTree
method
#19483
Conversation
props.items
in the getItemTree
method
Deploy preview: https://deploy-preview-19483--material-ui-x.netlify.app/ Bundle size report
|
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.
🙏
const getItemTree = React.useCallback(() => { | ||
const getItemFromItemId = (itemId: TreeViewItemId): TreeViewBaseItem => { | ||
const item = itemsSelectors.itemModel(store.state, itemId); | ||
const itemToMutate = { ...item }; |
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.
@flaviendelangle This only makes a clone of the outer-most item.
E.g:
const x = {a: {b: []}};
const copy = {...x};
copy.a.b = ['new'];
console.log(x)
// { a: { b: [ 'new' ] } }
Don't you need to use structuredClone
instead?
const x = {a: {b: []}};
const copy = structuredClone(x);
copy.a.b = ['new'];
console.log(x)
// { a: { b: [ 'new' ] } }
Edit: Sorry, my bad. This is a recursive function. Please ignore this comment.
Fixes #19482