Bird
Raised Fist0
React Nativemobile~20 mins

useState hook in React Native - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
useState Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ state_output
intermediate
2:00remaining
What is the output of this React Native component?

Consider this React Native component using useState. What will be displayed after pressing the button twice?

React Native
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';

export default function Counter() {
  const [count, setCount] = useState(0);

  return (
    <View>
      <Text>{count}</Text>
      <Button title="Add" onPress={() => setCount(count + 1)} />
    </View>
  );
}
A2
B1
C0
DButton press causes error
Attempts:
2 left
💡 Hint

Each button press increases the count by 1.

❓ component_behavior
intermediate
2:00remaining
How does this useState update behave inside a loop?

What will be the final value of count after pressing the button once in this React Native component?

React Native
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';

export default function LoopCounter() {
  const [count, setCount] = useState(0);

  const handlePress = () => {
    for (let i = 0; i < 3; i++) {
      setCount(count + 1);
    }
  };

  return (
    <View>
      <Text>{count}</Text>
      <Button title="Add 3" onPress={handlePress} />
    </View>
  );
}
A3
B0
C1
DButton press causes error
Attempts:
2 left
💡 Hint

Remember that state updates are asynchronous and do not immediately update count inside the loop.

📝 Syntax
advanced
2:00remaining
Which option correctly initializes useState with a function?

Which code snippet correctly uses useState with a function to set the initial state lazily?

Aconst [value, setValue] = useState(() => { expensiveComputation() });
Bconst [value, setValue] = useState(() => expensiveComputation());
Cconst [value, setValue] = useState(() => expensiveComputation);
Dconst [value, setValue] = useState(expensiveComputation());
Attempts:
2 left
💡 Hint

Lazy initialization requires passing a function that returns the initial value.

🔧 Debug
advanced
2:00remaining
Why does this useState update not reflect immediately in the UI?

In this React Native component, why does the displayed count not update immediately after calling setCount(count + 1) twice in a row?

React Native
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';

export default function DoubleIncrement() {
  const [count, setCount] = useState(0);

  const incrementTwice = () => {
    setCount(count + 1);
    setCount(count + 1);
  };

  return (
    <View>
      <Text>{count}</Text>
      <Button title="Increment Twice" onPress={incrementTwice} />
    </View>
  );
}
ABecause React Native does not support multiple state updates in one function.
BBecause setCount is asynchronous and the second call cancels the first.
CBecause the Button component does not trigger state updates properly.
DBecause both setCount calls use the same stale count value, so count only increases by 1.
Attempts:
2 left
💡 Hint

Think about how state values are captured in closures.

🧠 Conceptual
expert
3:00remaining
What is the best way to increment state multiple times in one event handler?

You want to increment a counter state three times in a single button press. Which approach ensures the count increases by 3?

ACall setCount(c => c + 1) three times in a row.
BCall setCount(count + 1) three times in a row.
CCall setCount(count + 3) once.
DCall setCount(() => count + 3) once.
Attempts:
2 left
💡 Hint

Remember how React batches state updates and how to use the updater function form.

Practice

(1/5)
1. What does the useState hook do in a React Native component?
easy
A. It fetches data from an external API automatically.
B. It styles the component with CSS properties.
C. It allows the component to remember and update values over time.
D. It creates a new screen in the app navigation.

Solution

  1. Step 1: Understand the purpose of useState

    The useState hook is designed to let components keep track of values that can change, like user input or toggles.
  2. Step 2: Compare options with useState's role

    Only It allows the component to remember and update values over time. correctly describes this behavior. Styling, fetching data, or navigation are handled by other tools.
  3. Final Answer:

    It allows the component to remember and update values over time. -> Option C
  4. Quick Check:

    useState manages state = A [OK]
Hint: Remember: useState stores and updates values inside components [OK]
Common Mistakes:
  • Confusing useState with styling or navigation
  • Thinking useState fetches data automatically
  • Believing useState creates new screens
2. Which of the following is the correct way to declare a state variable called count with initial value 0 using useState?
easy
A. const [count, setCount] = useState;
B. const [count, setCount] = useState(0);
C. const count, setCount = useState(0);
D. const count = useState(0);

Solution

  1. Step 1: Recall useState syntax

    The correct syntax uses array destructuring: const [state, setState] = useState(initialValue);
  2. Step 2: Check each option

    const [count, setCount] = useState(0); matches the correct syntax. const count = useState(0); misses destructuring, C uses comma incorrectly, and A misses parentheses.
  3. Final Answer:

    const [count, setCount] = useState(0); -> Option B
  4. Quick Check:

    useState syntax = D [OK]
Hint: Use array destructuring with useState and provide initial value [OK]
Common Mistakes:
  • Not using array destructuring
  • Forgetting parentheses after useState
  • Using commas instead of brackets
3. What will be displayed on the screen after the button is pressed twice in this React Native component?
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';

export default function Counter() {
  const [count, setCount] = useState(0);
  return (
    
      {count}
       setCount(count + 1)} />
    
  );
}
medium
A. 2
B. 0
C. 1
D. Error: setCount is not a function

Solution

  1. Step 1: Understand initial state and button action

    The initial count is 0. Each button press calls setCount(count + 1), increasing count by 1.
  2. Step 2: Calculate count after two presses

    After first press: count = 1. After second press: count = 2. The displayed text updates accordingly.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Two increments from 0 = 2 [OK]
Hint: Each press adds 1 to count starting from 0 [OK]
Common Mistakes:
  • Thinking count stays 0 without update
  • Confusing initial value with updated value
  • Expecting error due to misunderstanding setCount
4. Identify the error in this React Native code using useState:
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';

export default function Example() {
  const [value, setValue] = useState;
  return (
    
      {value}
       setValue('Hello')} />
    
  );
}
medium
A. useState is missing parentheses to call it as a function.
B. The setter function name should be getValue, not setValue.
C. The initial state value must be a number, not undefined.
D. The Button component cannot have an onPress prop.

Solution

  1. Step 1: Check useState declaration

    The code uses useState without parentheses, so it does not call the function to get state and setter.
  2. Step 2: Understand consequences

    Without calling useState(), value and setValue are undefined, causing errors when used.
  3. Final Answer:

    useState is missing parentheses to call it as a function. -> Option A
  4. Quick Check:

    useState() must be called with () [OK]
Hint: Always call useState with parentheses: useState() [OK]
Common Mistakes:
  • Forgetting parentheses after useState
  • Misnaming setter function
  • Assuming Button lacks onPress prop
5. You want to toggle a boolean state isVisible between true and false each time a button is pressed. Which is the best way to update the state using useState to ensure the toggle works correctly even if updates happen quickly?
const [isVisible, setIsVisible] = useState(false);
hard
A. setIsVisible(!isVisible);
B. setIsVisible(true);
C. setIsVisible(false);
D. setIsVisible(prev => !prev);

Solution

  1. Step 1: Understand state updates with previous value

    When toggling, using the previous state ensures correct flipping even if updates queue quickly.
  2. Step 2: Compare update methods

    setIsVisible(prev => !prev); uses a function to get previous state and invert it, which is reliable. setIsVisible(!isVisible); uses current state which may be stale. Options B and D set fixed values, not toggling.
  3. Final Answer:

    setIsVisible(prev => !prev); -> Option D
  4. Quick Check:

    Use functional update for toggling state [OK]
Hint: Use functional update: setState(prev => !prev) for toggles [OK]
Common Mistakes:
  • Using current state directly causing stale updates
  • Setting fixed true or false instead of toggling
  • Not using functional update for quick repeated changes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard