mui / mui/material-ui

[Box] Always re-renders sx is changed on the forward ref Div

Open
#32,627 1 comment 0 reactions 1 assignee View on GitHub

@siriwatknp is already working on this.

Since May 23, 2022.

component: Box performance scope: system type: new feature
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Duplicates
  • I have searched the existing issues
Latest version
  • I have tested the latest version
Current behavior 😯

Trying to mitigate re-rendering with Box. Normally wrapping in React.memo will do the job for most components

However with Box and Stack they forward their props to a div and so the Box/Stack won't re-render but the div will
causing the area to re-render always as well. Always getting props updated with (children, sx) changes were the sx is
always changing even then the sx value is memoized with useMemo which works on normal components.

const BOX = React.memo((props) => <Box {...props}/>

function MyComponent() {
   const [state, dispatch] = useReducer({input: ''}, (state, action) => ({...state, ...action}))
   
   const handleChange = useCallback((event) => dispatch({input: event.target.value}), [dispatch])
   
   const width = useMemo(() => ({minWidth: 120}), [])
   return (
   <>
      <Box>
         <TextField
           label={'input'}
           value={state.input}
           onChange={handleChange}
         />
      </Box>
      <Box sx={width}>
          Hello
      </Box>
   </>
   )
}
Expected behavior 🤔

For the wrapping of Box or Stack with React.memo to not cause re-rendering for that component if their props do not change.

Steps to reproduce 🕹

Steps:

  1. Make a component
  2. have an input or something that changes the components state to cause a component to re-render
  3. have a Box or Stack wrapped with React.memo with a useMemo sx prop
  4. using the React DevTools Profiler run some input changes and pause profile check and see that in fact the Box and Stack re-rendered
Context 🔦

There looks to be an issue with the forward ref for Box and Stack

Wanting to add React.memo to Box/Stack as to not always causing a re-render on these components and their children because of them wrapping their children.

I am not completely sure this is a super big deal but wanted to point it out. Its not that its a huge issue its usually .1 ms for each of these to render but if you have a lot that can be a problem. But was wondering if other components might have this issue.

Your environment 🌎
`npx @mui/envinfo`
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.