I'm new with React and I understand the benefits of the component based, inline styles. But I'm wondering if there is a decent way to have some sort of global style. For instance, I'd like to use the same Text and Button coloring throughout my app.
Rather than repeat in every component(and subsequently have to change it in x places if need be), my initial thought is to create a 'base' StyleSheet class in it own file and import it in my components.
Is there a better or more 'React' way?
To create global styles with React Native, we can create a module that exports the stylesheet object. import * as React from 'react'; import { View, Text, Button } from 'react-native'; import { Card } from 'react-native-paper'; import styles from './styles'; const App = () => { return ( <View> <Text style={styles.
To override style with React Native, we set the style prop to the styles we want by putting them in an object. const styles = StyleSheet. create({ CircleShapeView: { width: 50, height: 50, borderRadius: 50 / 2, backgroundColor: "#000", }, }); //...
app/components/Home/Home.component.style.js import {StyleSheet} from 'react-native'; import theme from '../../styles/theme. style'; import {headingText, textInput} from '../../styles/common. style'; export default StyleSheet. create({ container: { flex: 1, paddingVertical: theme.
You may create a reusable stylesheet. Example:
style.js
'use strict'; import { StyleSheet } from 'react-native'; module.exports = StyleSheet.create({ alwaysred: { backgroundColor: 'red', height: 100, width: 100, }, });
In your component:
const s = require('./style');
...then:
<View style={s.alwaysred} ></View>
Create a file for your styles (I.E., Style.js
).
Here is an example:
import { StyleSheet } from 'react-native'; export default StyleSheet.create({ container: { flex: 1 }, welcome: { fontSize: 20 } });
In any of the files you want to use your style, add the following:
import styles from './Style'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With