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

TouchableOpacity in React Native

Touchable opacity is the most suited example if you want to make views respond to proper touch. If you want to control the opacity then it can be done by...

3 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.

Difference Between React.js and React Native

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

3 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.

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.

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.

React Native Components

React Native components are the same as ordinary react components, but they are different in terms of rendering and styling. All react code lives in react components. They are independent...

16 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 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.

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.

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 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 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.

Basic knowledge of firebase for React Native

Firebase for react-native : Firebase is a mobile and web application development platform which is created in 2014 by google. It is a product of google which is offered to developers...

7 minutes read.

How Does React Native Work

Working of React Native React Native is a framework that lets us write android and iOS mobile applications using JavaScript. It is interesting to see how easy it is to develop...

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.

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.

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 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.

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.