Skip to content

Conversation

flaviendelangle
Copy link
Member

Fixes #19482

@flaviendelangle flaviendelangle self-assigned this Sep 8, 2025
@flaviendelangle flaviendelangle added type: bug It doesn't behave as expected. scope: tree view Changes related to the tree view. This includes TreeView, TreeItem. labels Sep 8, 2025
@flaviendelangle flaviendelangle changed the title [TreeView] Do not mutate props.items in the getItemTree method [TreeView] Do not mutate props.items in the getItemTree method Sep 8, 2025
@mui-bot
Copy link

mui-bot commented Sep 8, 2025

Deploy preview: https://deploy-preview-19483--material-ui-x.netlify.app/

Bundle size report

Bundle Parsed size Gzip size
@mui/x-data-grid 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-pro 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-premium 0B(0.00%) 0B(0.00%)
@mui/x-charts 0B(0.00%) 0B(0.00%)
@mui/x-charts-pro 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 0B(0.00%) 0B(0.00%)
@mui/x-tree-view 🔺+10B(+0.02%) 🔺+2B(+0.01%)
@mui/x-tree-view-pro 🔺+10B(+0.01%) 🔺+4B(+0.02%)

Details of bundle changes

Generated by 🚫 dangerJS against 6bbd36f

Copy link
Contributor

@noraleonte noraleonte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

@flaviendelangle flaviendelangle merged commit 890eeba into mui:master Sep 8, 2025
21 checks passed
@flaviendelangle flaviendelangle deleted the props-items-mutation branch September 8, 2025 11:29
const getItemTree = React.useCallback(() => {
const getItemFromItemId = (itemId: TreeViewItemId): TreeViewBaseItem => {
const item = itemsSelectors.itemModel(store.state, itemId);
const itemToMutate = { ...item };
Copy link

@caillou caillou Sep 9, 2025

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.

JCQuintas pushed a commit to JCQuintas/mui-x that referenced this pull request Sep 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: tree view Changes related to the tree view. This includes TreeView, TreeItem. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[tree view] The apiRef.current.getItemTree mutates props.items

4 participants