sx prop re-renders
See original GitHub issueI really like the API of being able to pass styles directly to components inline using sx
.
The one downside is that it can trigger re-renders on every parent render, since the styles create a new sx
object prop.
A few possible solutions:
- Follow a
styled
components type approach, and create astyled
function:
import { styled, Box } from 'dripsy'
const Page = styled(Box)(props => ({
backgroundColor: ['primary', 'secondary']
}))
- Could there be some sort of smart memoization / deep comparison that checks the
sx
prop, and doesn’t re-render if it matches? I am usually very, very way of deep comparison, as it’s not recommended by react in almost all cases (see here.)
One option could be to JSON.stringify the sx
prop and compare if this changed. The problem with that approach is that styles can a) contain animated values, and b) have functions:
// not JSON serializable...
<View sx={{ height: theme => theme.sizes[1] }} />
Basically, it would be nice if there were no concerns about passing style props inline for performance. I haven’t done extensive testing on this, but I imagine it could have some downsides.
Issue Analytics
- State:
- Created 3 years ago
- Comments:30 (29 by maintainers)
Top Results From Across the Web
css - extending sx props to memorize value and avoid rerenders
So I was wandering if there was a way to add a useMemo wrapped version of the MUI sx props. How would I...
Read more >React MUI CardMedia API - GeeksforGeeks
Props list: children: It is used to denote the content of the card. classes: It is to override or extend the styles applied...
Read more >How to customize - Material UI - MUI
The sx prop is the best option for adding style overrides to a single instance of a component in most cases. It can...
Read more >Overriding styles with the sx prop | Primer React
The sx prop allows ad-hoc styling that is still theme-aware. Declare the styles you want to apply in CamelCase object notation, ...
Read more >Prevent Unnecessary Component Rerenders with React memo
With React.memo , you can now pass a stateless functional component to it and it will ensure that it does not rerender unless...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Dripsy v3 will automatically memoize the
sx
prop under the hood usingstableHash
fromswr
. See here@nandorojo @cmaycumber Do you think a babel plugin like this might be worth?
Converts to
Cases
1. sx remains untouched if it contains any variables.
will be converted to
This case is considered because when it contains a variable, it gets harder to find location to put useMemo. e.g.
Here, we might have to move the variable
a
above the condition and add useMemo.2. Nit pick but it’s hard to tell if a function returning JSX will be used as a react component.
e.g.
can be used as
<Component />
orComponent();
This makes it difficult to recognize whether useMemo should be added or not, one heuristic I can think of is if a function name starts with a capital letter it can be considered as a react component and try to memoize sx props.