Navigate back to the homepage

React Native Styling

Infinitbility
Style
January 17th, 2021 · 1 min read
React Native Styling

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.

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

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

1import { StyleSheet} from 'react-native';

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

1import React from 'react';
2import { StyleSheet, Text, View } from 'react-native';
3
4const LotsOfStyles = () => {
5 return (
6 <View style={styles.container}>
7 <Text style={styles.red}>just red</Text>
8 <Text style={styles.bigBlue}>just bigBlue</Text>
9 <Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
10 {/* add multiple style in single component */}
11 <Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text>
12 </View>
13 );
14};
15
16const styles = StyleSheet.create({
17 container: {
18 marginTop: 50,
19 },
20 bigBlue: {
21 color: 'blue',
22 fontWeight: 'bold',
23 fontSize: 30,
24 },
25 red: {
26 color: 'red',
27 },
28});
29
30export default LotsOfStyles;

External Style

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

styles.js

1import { StyleSheet } from 'react-native';
2
3export default const styles = StyleSheet.create({
4 container: {
5 marginTop: 50,
6 },
7 bigBlue: {
8 color: 'blue',
9 fontWeight: 'bold',
10 fontSize: 30,
11 },
12 red: {
13 color: 'red',
14 },
15});

import style.js on your react native screen.

LotsOfStyles.js

1import React from 'react';
2import { Text, View } from 'react-native';
3import styles from 'styles';
4
5const LotsOfStyles = () => {
6 return (
7 <View style={styles.container}>
8 <Text style={styles.red}>just red</Text>
9 <Text style={styles.bigBlue}>just bigBlue</Text>
10 <Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
11 <Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text>
12 </View>
13 );
14};
15
16export default LotsOfStyles;

Use multiple styles in a single component

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

1{/* add multiple style in single component */}
2<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

1import { StyleSheet } from 'react-native';
2
3const TextStyles = StyleSheet.create({
4 bigBlue: {
5 color: 'blue',
6 fontWeight: 'bold',
7 fontSize: 30,
8 },
9 red: {
10 color: 'red',
11 },
12});
13
14const ScreenStyles = StyleSheet.create({
15 container: {
16 marginTop: 50,
17 },
18});
19
20export {TextStyles, ScreenStyles};

import on your react native screen

1import { 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

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

More articles from Infinitbility

React Native Props

React Native Props

React Native Tutorial - React Native Props

January 15th, 2021 · 2 min read
React Native State

React Native State

React Native Tutorial - React Native State

January 12th, 2021 · 1 min read
© 2021 Infinitbility
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]