Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native global styles

Tags:

react-native

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?

like image 254
Patm Avatar asked Jun 15 '15 19:06

Patm


People also ask

How do I use global style in React Native?

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.

How do you override CSS in React Native?

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", }, }); //...

How do you create a common style in React Native?

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.


2 Answers

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> 
like image 143
boredgames Avatar answered Oct 06 '22 07:10

boredgames


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' 
like image 36
Barlow Tucker Avatar answered Oct 06 '22 05:10

Barlow Tucker