States in React Native

We can control data of components of react-native by state and props. We are familiar with props and used them multiple times previously.

Props: This is used to customize components and make changes as per our needs. This remains the same for a whole lifetime of the component.  It is defined by parents.

State:  It can be changed. It’s not fixed like props.so if the data is going to be changed in the future, we will use state, and if the data is not going to change, we will use props.

We should make our state as simple as possible. 

The state is initialized inside the constructor, and setState is called whenever we want to change our state.

Example of  State :

import React, { Component, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
 
export default function App() {
  const[name,usename]= useState('react-native')
  return (
    <View style={styles.container}>
      <Text style={{fontSize: 50 ,fontWeight: 'bold'}}> I hope you will love this {name} tutorial </Text>
   
    </View>
  );
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor : '#50f9b7',
  },
});

The output of this code : 

States In React

Explanation : 

The basic structure of the code must be familiar to you.

Lets look at this part - 

const[name,usename]= useState('react-native')

Here we have initialized the name with ‘react-native ‘, but it is not like we would not be able to change it . we can change it by ‘ usename ‘ . let’s look  how  -

import React, { Component, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Button } from 'react-native-web';


export default function App() {
  const[name,usename]= useState('react-native')
  const clickHandler =()=>{
    usename("react-js");
  }
  return (
    <View style={styles.container}>
      <Text style={{fontSize: 50 ,fontWeight: 'bold'}}> I hope you will love this {name} tutorial </Text>
    <Button title='update tutorial name' onPress={clickHandler} />
    </View>
  );
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor : '#50f9b7',
  },
});

Output :

States In React

After clicking on the button  -

States In React

So lets see what happened here  -

Here additionally I have added-

const clickHandler =()=>{
    usename("react-js");
  }

And

onPress={clickHandler}

So you must know about this ‘onPress’ because we have already studied about it during buttons.

So it means whenever we click on the button – clickhandler will be called.

Within this ‘clickHandler’  we have used ‘usename’ and you must have seen that while declaring the name, we have also used usename there. So this ‘usename’ generally updates the name value . It’s not like we have to use only ‘ usename ‘ for updating. it depends upon you that what naming  convention you have used; you would understand this more clearly through practical example; let’s see

App.js code :

import React, { Component, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Button } from 'react-native-web';


export default function App() {
  const[name,hello]= useState('react-native')
  const clickHandler =()=>{
    hello("react-js");
  }
  return (
    <View style={styles.container}>
      <Text style={{fontSize: 50 ,fontWeight: 'bold'}}> I hope you will love this {name} tutorial </Text>
    <Button title='update tutorial name' onPress={clickHandler} />
    </View>
  );
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor : '#50f9b7',
  },
});

If you run this code, the output is still the same. But we have used ‘hello’ instead of ‘usename’, so it depends on what name you are giving it, but you have to use the method properly.

We have used state in switch . if you can recall you must have seen this code below  while studying switch –

App.js code :

import React, {useState} from 'react';
import { Switch, View,Text, SafeAreaView,StyleSheet} from 'react-native';
  const App = () => {
  const [switchValue, setSwitchValue] = useState(false);


  const toggleSwitch = (value) => {
    setSwitchValue(value);
    
  }
return (
    <SafeAreaView style={{flex: 1}}>
      <View style={styles.container}>
        <Text>
          {switchValue ? 'turned ON' : 'turned OFF'}
        </Text>
        <Switch
          style={{marginTop: 20}}
          onValueChange={toggleSwitch}
          value={switchValue}
        />
      </View>
    </SafeAreaView>
  );
};


const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor : '#eecafa'
  },
});


export default App;

The output of this code :

States In React
States In React

From the above explanation, I hope you understand why it’s working like this!

This was the simplest example of state! hope you learned things in the easiest manner.


Related Topics

React Native Pressable

It manages all the touch inputs, and it is one of the core wrappers introduced in July 2022. The Two important features of pressable are listed below- onPressInonPressOut onpressIn: We already know the...

3 minutes read.

React Native Alert

In this part of the tutorial, we will learn about react-native's alert API. It is compatible with both android and IOS. What is API? Answer - API stands for Application programming interface,...

4 minutes read.

React Native indicator

React native activity indicator : React native activity indicator is one of the react-native components. We all have seen it several times. It's the loader. Look at the picture below - So now...

4 minutes read.

React Native Fonts

The font is one of the important essences of any good UI(User Interface). It is very important to have a clear and beautiful font in your app. In this module,...

4 minutes read.

Why to Learn React Native

The demand for smartphone applications is ever-changing. The elder ones disappear from our remembrances, and smartphones and new applications come each day. In this turbulent ecosystem, it is struggling for...

4 minutes read.

Lifecycle of React Native Components

We are familiar with the word “lifecycle”. In human life, the lifecycle has three phases - birthlifedeath In react native its component lifecycle is almost the same as the human lifecycle. The...

6 minutes read.

HTTP in React Native

Networking HTTP in React-native: we all are aware that react native is a javascript framework.HTTP is nothing but an API request, and you might think that what is the need for an...

4 minutes read.

Redux in React Native

 It is a really amazing library of javascript, react, and react-native. It is very popular in react native domain. Every react and react native developer must know the redux it...

7 minutes read.

What is a Bridge in React Native?

A React Native is generally composed of two fields or two parts: The first part is the code of JavaScript.The second one is native code. The ability to build a bridge between...

3 minutes read.

React Native stack navigation

You must know about the stack data structure. Stack is one of the important data structures which works in the FILO(first in last out) method. As you know, mobile apps are...

4 minutes read.

React Native Sound

It is a great way to connect to the user. Just a tiny "ting" of any email received, or some sound for deleting a file or sound for sending a...

8 minutes read.

States in React Native

We can control data of components of react-native by state and props. We are familiar with props and used them multiple times previously. Props: This is used to customize components and...

3 minutes read.

React Native navigation bar and button

Here we will be focusing on header navigation. We will add buttons to the header, which is the best way to interact with a header. We have already learned about...

4 minutes read.

React Native Animations

Animation: animation is a method in which puppets, figures, drawings, etc., appear as moving creatures, making an illusion that things are moving. It is a unique way of expressing something...

6 minutes read.

React Native Side Drawer

In this part of the react-native tutorial, we will be learning about the react-native side drawer. We have already studied the status bar and various parts of react-native navigation, including...

5 minutes read.

React Native Tutorial

Introduction to react native : React-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. It is released by Facebook in 2015...

4 minutes read.

React native calender

In this article, we will be learning to add calendar features to react native app. So before creating the calendar, we do need to install a few packages or add some...

3 minutes read.

Advantages and Disadvantages of React Native

Advantages of using React Native It is observed that React Native stands out from most of the existing methods of cross-platform application development, like Ionic or Cordova, because React Native renders...

3 minutes read.

React Native Creating Components

React elements are where all React codes reside. The React elements are very similar to the regular React components, which have a unique style and offer contrast. Working with views When you...

3 minutes read.

React Native Status Bar

What is the status bar? The simplest and easiest answer to this question can be answered by looking at your mobile phone's top section. What can you see? Most probably, it's...

4 minutes read.