0
0
React Nativemobile~20 mins

useEffect hook in React Native - Practice Problems & Coding Challenges

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

Consider this React Native component using useEffect:

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

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

  useEffect(() => {
    const timer = setInterval(() => {
      setCount(c => c + 1);
    }, 1000);
    return () => clearInterval(timer);
  }, []);

  return (
    <View>
      <Text>Count: {count}</Text>
    </View>
  );
}

What will the Count text show after 3 seconds?

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

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

  useEffect(() => {
    const timer = setInterval(() => {
      setCount(c => c + 1);
    }, 1000);
    return () => clearInterval(timer);
  }, []);

  return (
    <View>
      <Text>Count: {count}</Text>
    </View>
  );
}
ACount: 4
BCount: 0
CCount: 1
DCount: 3
Attempts:
2 left
💡 Hint

Think about how many times the interval callback runs in 3 seconds.

lifecycle
intermediate
1:30remaining
When does this useEffect run?

Look at this React Native snippet:

useEffect(() => {
  console.log('Effect ran');
}, [props.value]);

When will the effect run?

React Native
useEffect(() => {
  console.log('Effect ran');
}, [props.value]);
AOnly when props.value changes
BEvery time the component renders
COnly when the component mounts
DNever
Attempts:
2 left
💡 Hint

Check the dependency array.

📝 Syntax
advanced
2:00remaining
What error does this code raise?

Examine this React Native code using useEffect:

useEffect(() => {
  async function fetchData() {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    setData(data);
  }
  fetchData();
}, []);

What error will this code produce?

React Native
useEffect(() => {
  async function fetchData() {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    setData(data);
  }
  fetchData();
}, []);
AReferenceError: setData is not defined
BNo error, runs correctly
CTypeError: fetchData is not a function
DSyntaxError: useEffect callback cannot be async
Attempts:
2 left
💡 Hint

Check if all variables used are declared.

state_output
advanced
2:30remaining
What is the final value of count after clicking the button twice?

Consider this React Native component:

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

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

  useEffect(() => {
    setCount(count + 1);
  }, [count]);

  return (
    <View>
      <Text>Count: {count}</Text>
      <Button title="Click me" onPress={() => setCount(count + 1)} />
    </View>
  );
}

What happens after clicking the button two times?

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

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

  useEffect(() => {
    setCount(count + 1);
  }, [count]);

  return (
    <View>
      <Text>Count: {count}</Text>
      <Button title="Click me" onPress={() => setCount(count + 1)} />
    </View>
  );
}
ACount stays the same
BCount increases indefinitely causing a crash
CCount increases by 2 and stops
DCount increases by 1 and stops
Attempts:
2 left
💡 Hint

Think about what happens when count changes and triggers useEffect.

🔧 Debug
expert
3:00remaining
Why does this useEffect not update the displayed data?

Here is a React Native component fetching data:

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

export default function DataFetcher() {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(json => {
        data = json;
      });
  }, []);

  return (
    <View>
      <Text>{data ? data.message : 'Loading...'}</Text>
    </View>
  );
}

Why does the component never show the fetched data?

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

export default function DataFetcher() {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(json => {
        data = json;
      });
  }, []);

  return (
    <View>
      <Text>{data ? data.message : 'Loading...'}</Text>
    </View>
  );
}
ABecause fetch is not awaited
BBecause useEffect has an empty dependency array
CBecause data is assigned directly instead of using setData
DBecause data.message does not exist
Attempts:
2 left
💡 Hint

How do you update state in React?