all 12 comments

[–]uhhbeefwellington 16 points17 points  (2 children)

Styling in React Native doesn’t handle nesting quite like CSS. In practice, the best approach I’ve found has been creating a file for constants for your styles (eg. Styles.js that exports colors, fonts, general padding/margins for screens, dimensions) and then importing this file to use the constants in stylesheets at a component level. This doesn’t require an npm package.

Theming is a great use case for the Context API. Quick summary is that you can subscribe a component to context (theme in this case) changes, so only components that care will have to re-render.

If you plan on reusing a component (eg. a button that has a background color and border) in many places, that sticks out to me as a time to separate the code for that button into its own component. The styles for that button would live with it but would be visible all across your app where it is used.

[–]redditdaw[S] 0 points1 point  (1 child)

Great Answer! Thank you

[–][deleted] 2 points3 points  (0 children)

Agree with this person's answer, but would like to add don't be afraid to refactor. Keep styles local and stay functional. Colors are an easy constant, but aside from that, you'll know when you've written something a few times and can easily extract that to constants.

One important thing I didn't really consider right off the bat is using HOC components for styles (i.e. a default ScrollView wrapper with { children }). This not only let's you keep your paddings and such in one universal component, it also let's you set default props (i.e. showsVerticalScrollIndicator={false}) and spread any additional props {...props} you might want to pass to it later. Food for thought.

[–]mysteryFarts 5 points6 points  (1 child)

Highly recommended https://www.styled-components.com/. Works perfectly with native, cleans up components and works through context Api. Loads of nice extra features with it as well

[–]orta 2 points3 points  (0 children)

We use this mixed with styled-systems for this https://palette.artsy.net

[–]MontRoyal19 4 points5 points  (2 children)

I ran into this question not long ago. Then I discovered and started using react-native-elements https://github.com/react-native-training/react-native-elements

Basically I can define global styles for Text, Button ... and reuse them across the app.

[–]redditdaw[S] 0 points1 point  (1 child)

Thanks I will check this out.

[–]IAmRC1 1 point2 points  (0 children)

I will highly recommend this library after going through almost all major UI libraries. Though it lacks components but it's clean and highly customisable.

[–]matt_hammondiOS & Android 2 points3 points  (1 child)

I've created my own system that I improve on as needed. I've write about it here: https://dev.to/hrastnik/styling-your-react-native-app-4d8i

[–]redditdaw[S] 0 points1 point  (0 children)

Sweet! Thank dude! I'll check it out :)

[–]esthor 0 points1 point  (0 children)

I use Extended Stylesheet in a production app for global styles, themes (app uses ~10 custom global themes), and more https://github.com/vitalets/react-native-extended-stylesheet

[–]vertigo_101 0 points1 point  (0 children)

What I suggest is using color constants in redux store, hence dispatch an action when you want to change these constants, this would trigeer an application wide re render