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.
- Inline Style
- Internal style
- External Style
- Use multiple styles in a single component
- 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';34const 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};1516const 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});2930export 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';23export 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';45const 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};1516export 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';23const TextStyles = StyleSheet.create({4 bigBlue: {5 color: 'blue',6 fontWeight: 'bold',7 fontSize: 30,8 },9 red: {10 color: 'red',11 },12});1314const ScreenStyles = StyleSheet.create({15 container: {16 marginTop: 50,17 },18});1920export {TextStyles, ScreenStyles};
import on your react native screen
1import { TextStyles, ScreenStyles } from 'styles';
More From React Native Tutorial
Basics
- Introduction To React Native
- React Native Environment Setup using expo
- React Native Environment Setup for windows
- React Native Environment setup on Mac OS
- React Native Environment setup on linux
- React Native Project Structure
- React Native State
- React Native Props
- React Native Styling
- React Native Flexbox
- React Native Text
- React Native Textinput
- React Native Commands
- React Native ScrollView
Advances
- React Native Dark Mode
- React Native Fonts
- React Native SQLite
- React Native DatepickerAndroid
- React native ScrollView scroll to position
- How to align icon with text in react native
- React Native Image
- React Native Firebase Crashlytics
- React Native Async Storage
- React Native Share
Error & Issue Solution
- Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native
- Expiring Daemon because JVM heap space is exhausted In React Native
- Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native
- Unable to determine the current character, it is not a string, number, array, or object in react native
- App crashed immediately after install react native video or track player
- how to delete SQLite database in android react native
- React native material dropdown twice click issue
- How to get the current route in react-navigation?
- how to disable drawer on the drawer navigation screen?
- Image not showing in ios 14 react native