This Article Part of React Native Tutorial Series want to start from scrach follow below link

https://infinitbility.com/react-native/table-of-contents

Today, we learn how many ways to write CSS in react native. the CSS in react a native little bit different from actual CSS.

  1. Inline Style
  2. Internal style
  3. External Style
  4. Use multiple styles in a single component
  5. Create multiple styles constant and import

Inline Style

However, this is not the best practice because it can be hard to read the code. but for a knowledge check out the below example.

You can add styling to your component using style props you simply add style props to your element it accepts an object of properties.

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
export default class App extends Component<Props> {
    render() {
        return (
            <View style={{flex:1,justifyContent:"center",backgroundColor:"#fff", alignItems:"center"}}>
                <View style={{width:200,height:150,backgroundColor:"red",padding:10}}>
                    <Text style={{fontSize:20, color:"#666"}}>Inline Style</Text>
                </View>
            </View>
        );
    }
}

Internal style

if you have a large code base or you want to set many properties to your elements, writing our styling rules directly inside style props will make our code more complex that’s why React Native give us another way that let us write a concise code using StyleSheet method: First, make sure to import StyleSheet from react-native

import { StyleSheet} from 'react-native';

And try to assign some style properties using create method that takes an object of properties.


import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

const LotsOfStyles = () => {
    return (
      <View style={styles.container}>
        <Text style={styles.red}>just red</Text>
        <Text style={styles.bigBlue}>just bigBlue</Text>
        <Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
        {/* add multiple style in single component */}
        <Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text>
      </View>
    );
};

const styles = StyleSheet.create({
  container: {
    marginTop: 50,
  },
  bigBlue: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
  },
  red: {
    color: 'red',
  },
});

export default LotsOfStyles;

External Style

External Style is the best way to write & organize CSS in react native. learn with the below example.

styles.js


import { StyleSheet } from 'react-native';

export default const styles = StyleSheet.create({
  container: {
    marginTop: 50,
  },
  bigBlue: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
  },
  red: {
    color: 'red',
  },
});

import style.js on your react native screen.

LotsOfStyles.js

import React from 'react';
import { Text, View } from 'react-native';
import styles from 'styles';

const LotsOfStyles = () => {
    return (
      <View style={styles.container}>
        <Text style={styles.red}>just red</Text>
        <Text style={styles.bigBlue}>just bigBlue</Text>
        <Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
        <Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text>
      </View>
    );
};

export default LotsOfStyles;

Use multiple styles in a single component

many time we need to multiple styles class for single component, learn with below example.

{/* add multiple style in single component */}
<Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text>

Create multiple styles constant and import

Learn below example on how to create multiple styles constant & import in your react native screen.

styles.js


import { StyleSheet } from 'react-native';

const TextStyles = StyleSheet.create({
  bigBlue: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
  },
  red: {
    color: 'red',
  },
});

const ScreenStyles = StyleSheet.create({
    container: {
        marginTop: 50,
    },
});

export {TextStyles, ScreenStyles};

import on your react native screen

import { TextStyles, ScreenStyles } from 'styles';

More From React Native Tutorial

Basics

1. Introduction To React Native

2. React Native Environment Setup using expo

3. React Native Environment Setup for windows

4. React Native Environment setup on Mac OS

5. React Native Environment setup on linux

6. React Native Project Structure

7. React Native State

8. React Native Props

9. React Native Styling

10. React Native Flexbox

11. React Native Text

12. React Native Textinput

13. React Native Commands

14. React Native ScrollView

Advances

1. React Native Dark Mode

2. React Native Fonts

3. React Native SQLite

4. React Native DatepickerAndroid

5. React native ScrollView scroll to position

6. How to align icon with text in react native

7. React Native Image

8. React Native Firebase Crashlytics

9. React Native Async Storage

10. React Native Share

Error & Issue Solution

1. Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native

2. Expiring Daemon because JVM heap space is exhausted In React Native

3. Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native

4. Unable to determine the current character, it is not a string, number, array, or object in react native

5. App crashed immediately after install react native video or track player

6. how to delete SQLite database in android react native

7. React native material dropdown twice click issue

8. How to get the current route in react-navigation?

9. how to disable drawer on the drawer navigation screen?

10. Image not showing in ios 14 react native