Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge / Combine two or more different StyleSheet components in React Native?

Tags:

I'm separating my styles in the following way:

styles / |-- base.js |-- base.ios.js |-- base.android.js 

Each of them exports a StyleSheet component created as in this example:

import { StyleSheet } from 'react-native';  export default StyleSheet.create({     statusBar: {     height: 20 }); 

How can I merge them so I only have one base style object? I'm looking for something like:

const baseStyles = mergeStyles(baseStyle, platformStyle); 
like image 440
R01010010 Avatar asked Dec 22 '16 18:12

R01010010


People also ask

How do you merge two styles in React Native?

Use the spread syntax to combine multiple inline style objects in React, e.g. style={{...style1, ... style2}} . The spread syntax will unpack the key-value pairs of the objects into a final object and the styles will get applied to the element.

How do you define global styles 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.


1 Answers

you are very close:

const baseStyles = [baseStyle, platformStyle]; 

basically any component can cascade styles like this:

<View style={[styles.style1,styles.style2]}></View> 
like image 54
Tarek Avatar answered Oct 21 '22 07:10

Tarek