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 to manage to react native state.
React Native manage data using state and props. props mean those data, send data to the child component from the parent component but we can discuss props in our next coming tutorial.
state
we are using state to store data and accessible on the whole file. when you store Anything on state then you can use it anywhere you want like functions, send data to the server, etc.
Anything that changes over time is known as state.
setState
setState() method use to store or update data on state.
React Native create state example
below example explain how to create state on react native.
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class App extends React.Component {
this.state = {
text: 'React Native State Example'
}
render() {
return (
<View>
<Text> {this.state.text} </Text>
</View>
);
}
}
React Native update state example
below example explain how to update state on react native.
import React, { Component } from 'react'
import { Text, View } from 'react-native'
class App extends Component {
state = {
text: 'React Native State Example'
}
updateState = () ⇒ this.setState({ text: 'React Native State Example Updated' })
render() {
return (
<View>
<Text onPress = {this.updateState}>
{this.state.text}
</Text>
</View>
);
}
}
export default App;
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?