0
0
React Nativemobile~20 mins

useState hook in React Native - Practice Problems & Coding Challenges

Choose your learning style9 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.