If you are new to React Native, I will recommend you to read the previous article of this series.
In React (or React Native), there are two types of data that can control a component - State and Props.
State
State can be referred to as the local state of the component. The state of a component cannot be accessed and modified outside of the component. State is mutable, which means that the state of a component can be changed in response to some action. The state of one component can behave as Props for the other component as well. We cannot update the state directly. So, in order to update the state of the component, we have to use the setState method. setState re-renders the parent component as well as the child component class.
Let’s see how we can use State in a React Native application.
App.js Code
- import React from 'react';
- import { StyleSheet, Text, View } from 'react-native';
-
- export default class App extends React.Component {
- state={
- Counter:1
- }
- updateState=()=>
- this.setState({Counter:this.state.Counter+1})
- render() {
- return (
- <View style={styles.container}>
- <Text style={{fontSize:40}}>State Example</Text>
- <Text style={{fontSize:40}} onPress={this.updateState}>
- {this.state.Counter}
- </Text>
- </View>
- );
- }
- }
-
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- backgroundColor: '#fff',
- alignItems: 'center',
- justifyContent: 'center',
- },
- });
In the above example, we have created a State object and added a Counter property with initial value as 1. We have declared an updateState function in order to update the state using the setState method. On calling setState, it will re-render the component. On the text component, we are using an onPress Touch event with which we can use the updateState function and increase the counter value with 1. Now, let’s run the application with the npm start command.
Preview
Props
Props stands for the properties and is used to pass the data as well as an event handler to the child components (passing data from the view to the controller or from parent to the child). It is immutable, that means we cannot change its value. Interestingly, it has faster performance than State. Let’s make some changes in the previous example in order to check its functionality. In order to demonstrate, we need two classes, one will behave as a parent and the other one will behave as a child.
- import React from 'react';
- import { StyleSheet, Text, View } from 'react-native';
- import Child from './ChildApp';
-
- export default class App extends React.Component {
- state={
- Counter:1
- }
-
- updateState=()=>this.setState({Counter:this.state.Counter+1})
- render() {
- return (
- <View style={styles.container}>
- <Child myCounter={this.state.Counter} updateState={this.updateState}></Child>
- </View>
- );
- }
- }
-
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- backgroundColor: '#fff',
- alignItems: 'center',
- justifyContent: 'center',
- },
- });
In the above code, we are importing the Child class (code for the Child class is provided below) and passing the Counter value as myCounter attribute in JSX as well as function as updateState atribute for the updating the State as props.
- import React from 'react';
- import { StyleSheet, Text, View } from 'react-native';
-
- export default class Child extends React.Component {
- render() {
- return (
- <View style={styles.container}>
- <Text style={{fontSize:40}}>Props Example</Text>
- <Text style={{fontSize:40}} onPress={this.props.updateState}>
- {this.props.myCounter}
- </Text>
- </View>
- );
- }
- }
-
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- backgroundColor: '#fff',
- alignItems: 'center',
- justifyContent: 'center',
- },
- });
As we know, our Child class is receiving multiple props, so can use this.props.<Name_Of_Property> in order to access its value.
Preview
Hope this will help you.
Thanks.