This Article Part of React Native Tutorial Series want to start from scrach follow below link
https://infinitbility.github.io/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.
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
Advances
4. React Native DatepickerAndroid
5. React native ScrollView scroll to position
6. How to align icon with text in react native
8. React Native Firebase Crashlytics
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
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?