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

[HELP] how to handle react-perf/jsx-no-new-object-as-prop while passing array of styles to react native component? #44

Closed
TheSolly opened this issue Jan 24, 2021 · 5 comments

Comments

@TheSolly
Copy link

So, considering the code below, which is supposedly popular in react-native, how should I re-design my code to remove the warning?

<Text
              style={[
                styles.smallBoldText,
                styles.greenText,
                styles.greenBackground,
              ]}>
              Some Text
</Text>
@bolatovumar
Copy link

@TheSolly I don't know anything about React Native specifically but in the web version of react you could either create a constant outside of the component if these are all just static properties. If styles is passed as a prop or something you could use useMemo to memoize this array.

@cvazac
Copy link
Owner

cvazac commented Jan 26, 2021

Both excellent suggestions, @bolatovumar.

@TheSolly
Copy link
Author

TheSolly commented Jan 26, 2021

Understood, thanks a lot for explaning @bolatovumar 🙏
And to understand even more, what is the benefit of doing so ( Memoization or const)? is it just performance related?

@bolatovumar
Copy link

Understood, thanks a lot for explaning @bolatovumar 🙏
And to understand even more, what is the benefit of doing so ( Memoization or const)? is it just performance related?

@TheSolly Yes, if React Native works in the same way as React for web then you should get some perf improvement if the component in question tends to re-render a lot.

@douglasjunior
Copy link

Here we use with useMemo:

const textStyles = useMemo(() => [
                styles.smallBoldText,
                styles.greenText,
                condition ? styles.greenBackground : null,
], [condition]);


return (
    <Text
              style={textStyles}>
              Some Text
    </Text>
);

@cvazac cvazac closed this as completed Jan 11, 2024
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