Skip to content
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

Avoid gratuitous re-rendering of components using settings or theme #4619

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mannby
Copy link

@mannby mannby commented Feb 4, 2025

Motivation

If an app has components surrounding PaperProvider that occasionally refresh, then components that use the theme provided by PaperProvider will refresh even if they don't need to. E.g. { animation: { scale: 1 } } changes every time because object equality is violated, even though the theme object contents are the same from render to render.

The theme is unlikely to change often, so memoizing it should not have much memory overhead.

Related issue

n/a

Test plan

An app's appearance should continue to follow theme changes, but the react-devtools profiler should not show components re-rendering because of theme (or settings) changes when there haven't been any, even if a PaperProvider parent is re-rendered.

One can also use a hook like the following to look for changes to a theme fetched with useTheme():

export const useTraceUpdate = (v: any) => {
const prev = React.useRef(v)
React.useEffect(() => {
// Compare v and prev, output differences, etc.
prev.current = v
}, [v])
}

useTraceUpdate(theme)

Passes lint and test

@callstack-bot
Copy link

Hey @mannby, thank you for your pull request 🤗. The documentation from this branch can be viewed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants