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

no-unused-styles reports false positive when used inside React.useMemo #276

Open
janKir opened this issue Dec 9, 2020 · 3 comments
Open

Comments

@janKir
Copy link

janKir commented Dec 9, 2020

When I memoize a Stylesheet that derives some styles from props or state, the react-native/no-unused-styles reports an error although the memoized object is used.

Unused style detected: undefined.box eslint(react-native/no-unused-styles)

Example:

export const Foo = ({bar}) => {
  const styles = React.useMemo(() => Stylesheet.create({
    box: {
      margin: bar,
    },
  }),[bar]);

  return <View style={styles.box} />;
}
@gentlee
Copy link

gentlee commented Aug 19, 2022

Never use StyleSheet.create inside useMemo. Need to create rule for that.

@vdyalex
Copy link

vdyalex commented Aug 24, 2022

The assumption is correct. Dynamic styles should be memoized to prevent triggering re-renders. A simple benchmark with Why Did You Render would show that. The most expensive operation for RN is converting back and forth data to JSON to propagate the unnecessary changes over the bridge, therefore YES: memoize your dynamic styles.

@apapalillo
Copy link

I totally stand with this. I often need to move some styles in the component to make a dynamic stylesheet object, but I also need to memoize them for performance purposes.

So this is definitely an issue.

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

No branches or pull requests

4 participants