React Native Functional Component with useState

What is useState?

In react-native props and state are the data which are used to handle the component. The component will update itself as soon as the state changes its value. 

useState is a hook that is used to define the state for the functional component. Using useState you can define the initial value for the state and update it later for the functional component.

How to use useState?

import React, { useState} from 'react';
const [loading, setLoading] = useState(true); 

The above is the syntax to define and used the hook in the functional component. We have defined the initial value is true for the above state.
  • loading : The name of the state.
  • setLoading: Method definition to update the value of the state later in the code.
  • useState: Define the const to work as the state and pass the initial value to it.

How to update the value for the useState?

To update the state value you can see the setLoading method defined in the example.

setLoading(false)  




Comments