[Box] Always re-renders sx is changed on the forward ref Div
@siriwatknp is already working on this.
Since May 23, 2022.
- 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:
- Make a component
- have an input or something that changes the components state to cause a component to re-render
- have a
BoxorStackwrapped with React.memo with a useMemo sx prop - 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.