The useState hook is used to manage state in functional components in React Native. It allows components to store, update, and react to changing data such as counters, form inputs, toggles, and UI state.
useState returns a state value and a setter functionThe useState hook is imported from React and called inside a functional component.
// Basic syntax of useState hook
const [state, setState] = useState(initialValue);
// Counter & Toggle example using useState in React Native
import React, { useState } from "react";
import { View, Text, Button } from "react-native";
export default function App() {
// 1. Number State
const [count, setCount] = useState(0);
// 2. Boolean State
const [isActive, setIsActive] = useState(false);
return (
<View>
{/* Counter Logic */}
<Text>Count: {count}</Text>
<Button title="Increase" onPress={() => setCount(count + 1)} />
{/* Toggle Logic */}
<Text>Status: {isActive ? "Active" : "Inactive"}</Text>
<Button
title="Toggle"
onPress={() => setIsActive(!isActive)}
/>
</View>
);
}
Test the logic below. This simulates two independent state variables: a number (Count) and a boolean (Status).
States are independent. Updating one does not reset the other.
0. Clicking "Increase" calls setCount(prev + 1).false. Clicking "Toggle" calls setIsActive(!isActive).Each button press calls its specific setter function. Notice how the "Count" and the "Status" do not interfere with each other. This is because you can have multiple useState hooks in a single component.
useState