Thuta Learning
React Native
IntermediateMobile DevelopmentIntermediate

State with useState Hook

React NativeLesson 820 min

What you'll walk away with

  • Explain the React and native-platform behavior of State with useState Hook
  • Test accessibility, failure, and lifecycle cases
  • Write maintainable, testable, and performant React Native

Explain it simply

State with useState Hook, in plain language

The useState hook is used to track local state within a component — every time the state value changes, it triggers a re-render of the component. React Native imports React's hooks system directly too, so the syntax is exactly the same as web React — the difference lies in UI events (onPress vs onClick) and components (TouchableOpacity vs button).

Problem → Why → Solution

The problem — why this concept matters

Trying to mutate state directly (isLiked = true) — you should always use the setIsLiked function

A common mistake

Calling the function immediately inside onPress (onPress={setIsLiked(!isLiked)}) — you need to wrap it in an arrow function (onPress={() => setIsLiked(!isLiked)})

Core concept — understand how it works

For State with useState Hook, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.

Mental model
  1. JavaScript state
  2. native component and layout
  3. Android or iOS UI

Explain the React and native-platform behavior of State with useState Hook

Core syntax or pattern

javascript
import { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';

export default function LikeButton() {
  const [isLiked, setIsLiked] = useState(false);

  return (
    <TouchableOpacity onPress={() => setIsLiked(!isLiked)}>
      <Text style={[styles.heart, isLiked && styles.liked]}>
        {isLiked ? '❤️' : '🤍'}
      </Text>
    </TouchableOpacity>
  );
}

const styles = StyleSheet.create({
  heart: { fontSize: 32 },
  liked: { transform: [{ scale: 1.2 }] },
});

For State with useState Hook, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.

Read the code step by step

  1. JavaScript stateimport { useState } from 'react';

    The useState hook is used to track local state within a component — every time the state value changes, it triggers a re-render of the component. React Native imports React's hooks system directly too, so the syntax is exactly the same as web React — the difference lies in UI events (onPress vs onClick) and components (TouchableOpacity vs button).

  2. native component and layout</Text>

    For State with useState Hook, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.

  3. Android or iOS UI});

    Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Execution flow
  1. JavaScript state
  2. native component and layout
  3. Android or iOS UI

Success

Explain the React and native-platform behavior of State with useState Hook

Failure

Trying to mutate state directly (isLiked = true) — you should always use the setIsLiked function

Let's think about this for a moment

The useState hook is used to track local state within a component — every time the state value changes, it triggers a re-render of the component. React Native imports React's hooks system directly too, so the syntax is exactly the same as web React — the difference lies in UI events (onPress vs onClick) and components (TouchableOpacity vs button).

Let's connect this to a real scenario

To build a like button, you create an isLiked state with useState(false), then call setIsLiked(!isLiked) inside TouchableOpacity's onPress — every press toggles the state and can change the icon color. You'll combine the style conditionally based on state (using the array syntax from the previous lesson).

Code Example

javascript
import { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';

export default function LikeButton() {
  const [isLiked, setIsLiked] = useState(false);

  return (
    <TouchableOpacity onPress={() => setIsLiked(!isLiked)}>
      <Text style={[styles.heart, isLiked && styles.liked]}>
        {isLiked ? '❤️' : '🤍'}
      </Text>
    </TouchableOpacity>
  );
}

const styles = StyleSheet.create({
  heart: { fontSize: 32 },
  liked: { transform: [{ scale: 1.2 }] },
});
You should see
Every time you press the heart icon, you'll see it toggle between filled and outline states.

5-Minute Try-It

Build a counter app — create a count state with useState(0) and implement it so count increases every time you press the '+' button.

A Quick Heads-Up

Writing onPress={setIsLiked(!isLiked)} calls the function immediately on every render and can cause an infinite loop — you need to wrap it with an arrow function () => ...

Real-world usage

How to apply it in a real project

Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Mini challenge

Build it yourself

Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
javascript
import { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';

export default function LikeButton() {
  const [isLiked, setIsLiked] = useState(false);

  return (
    <TouchableOpacity onPress={() => setIsLiked(!isLiked)}>
      <Text style={[styles.heart, isLiked && styles.liked]}>
        {isLiked ? '❤️' : '🤍'}
      </Text>
    </TouchableOpacity>
  );
}

const styles = StyleSheet.create({
  heart: { fontSize: 32 },
  liked: { transform: [{ scale: 1.2 }] },
});

Expected: Explain the React and native-platform behavior of State with useState Hook

Show hint

Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Quick quiz 1 / 3

What best describes the core idea of State with useState Hook?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • JavaScript state → native component and layout → Android or iOS UI
  • For State with useState Hook, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
  • Trying to mutate state directly (isLiked = true) — you should always use the setIsLiked function
  • Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

React Native Documentation — React Native

Easy traps

  • Trying to mutate state directly (isLiked = true) — you should always use the setIsLiked function
  • Calling the function immediately inside onPress (onPress={setIsLiked(!isLiked)}) — you need to wrap it in an arrow function (onPress={() => setIsLiked(!isLiked)})
  • Assuming one simulator's happy path proves every real-device, release-build, permission, accessibility, offline, and lifecycle path.

Hands-on Exercise

Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

You'll know it worked when: Every time you press the heart icon, you'll see it toggle between filled and outline states.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

State with useState Hook | Thuta Learning