React Native Tutorial
- React Native Tutorial
- React Native Environment Setups
- React Native First Application Hello World
- React Native View
- React Native State
- React Native Props
- React Native Style
- React Native Height and Width
- React Native Button
- React Native Layout and Flexbox
- React Native Positioning Element with Flex
- React Native ScrollView
- React Native ListView
- React Native FlatList
- React Native SectionList
- React Native Touchables
- React Native Text Input
- React Native ActivityIndicator
- React Native Picker
- React Native StatusBar
- React Native Switch
- React Native WebView
- React Native ProgressBarAndroid
- React Native ProgressBar With Animated
Navigation
- React Native Navigation
- React Native Configuring Header Bar
- React Native Moving Between Screens
- React Native Passing Value between Screen
- React Native Tab Navigation
- React Native Adding Icons at the Bottom of Tab Navigation
- React Native Create Material Bottom Tab Navigator
- React Native Top Tab Navigator
- React Native Drawer Navigation
Storage
React Misc
- React Native Google Map
- React Native Modal
- React Native Vector Icons
- React Native Splash Screen
- React Native vs. Ionic
- React Native vs. Xamarin
- React Native vs Flutter
- React Native vs React
- React Native vs Swift
- Box shadow in React Native
- React Native IAP
- React-Native Localization
- React Native Toast
- React Native Sound
React Native Style
React Native simply uses JavaScript for styling our core components. We don't require any special language or syntax for defining styles. All the core components use a prop (property) named style. The style names and values are similar to CSS that works for the web. There is the only difference in the way of writing names using camel casing, e.g fontSize rather than font-size.
The style prop is a plain old JavaScript object which is the simplest way for styling our code.
There are different ways to design our component, by using inline style and by using StyleSheet.create. As the component increases in complexity, it is better to use StyleSheet.create to define several styles in one place.
React Native style Example 1
In this example, we will use both inline style as well as StyleSheet.create. Inline styles are applied at where the components are created.
PauseNext
Unmute
Current Time 0:03
/
Duration 18:10
Loaded: 3.30%
Â
Fullscreen
App.js
import { AppRegistry, StyleSheet, Text, View } from 'react-native';
export default class ImplementingStyle extends Component {
render() {
return (
<View>
<Text style={{ backgroundColor: '#a7a6a9', color: 'yellow', fontSize: 20 }}>this is inline style</Text>
<Text style={styles.green}>just green</Text>
<Text style={styles.biggray}>just biggray</Text>
<Text style={[styles.biggray, styles.green]}>biggray, then green</Text>
<Text style={[styles.green, styles.biggray]}>green, then biggray</Text>
</View>
);
}
}
const styles = StyleSheet.create({
biggray: {
color: 'gray',
fontWeight: 'bold',
fontSize: 30,
},
green: {
color: 'green',
},
});
Output
React Native style Example 2
We can also use the external JavaScript file to style our application. In this example, we create external JS file and import it into our App.js file.
StyleComponent.js
import { Text, View, StyleSheet } from 'react-native'
const StyleComponent = (props) => {
return (
<View>
<Text style = {styles.myState}>
{props.myState}
</Text>
</View>
)
}
export default StyleComponent
const styles = StyleSheet.create ({
myState: {
marginTop: 20,
textAlign: 'center',
color: 'green',
fontWeight: 'bold',
fontSize: 20
}
})
App.js
import { StyleSheet, Text, View } from 'react-native';
import StyleComponent from './StyleComponent'
export default class App extends React.Component {
state = {
myState: 'This is my state, style through external style'
}
render() {
return (
<View>
<StyleComponent myState = {this.state.myState}/>
</View>
);
}
}
Output