In React Native, both state and variables are used to store data. However, they behave very differently when data changes and when the UI needs to update. Understanding this difference is critical for building dynamic mobile applications.
In React Native functional components, state is created using the useState hook. Variables are declared using let, const, or var just like normal JavaScript.
// Import required hooks and components
import React, { useState } from "react";
import { View, Text, Button } from "react-native";
export default function App() {
// Normal JavaScript variable
let countVariable = 0;
// React state variable
const [countState, setCountState] = useState(0);
return (
<View>
<Text>Variable Count: {countVariable}</Text>
<Text>State Count: {countState}</Text>
<Button
title="Increase Variable"
onPress={() => {
countVariable = countVariable + 1;
}}
/>
<Button
title="Increase State"
onPress={() => {
setCountState(countState + 1);
}}
/>
</View>
);
}
Think of state as a remote control connected to the screen. Whenever state changes, React re-renders the screen. A normal variable is like writing on paper — React never notices the change.