0
0
React Nativemobile~20 mins

Lifting state up in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output when lifting state up in this React Native example?

Consider two sibling components that share a counter state lifted up to their parent. What will be displayed when you press the increment button in one sibling?

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

function SiblingA({ count, onIncrement }) {
  return <Button title={`Increment A (${count})`} onPress={onIncrement} />;
}

function SiblingB({ count }) {
  return <Text>Count in B: {count}</Text>;
}

export default function Parent() {
  const [count, setCount] = useState(0);
  return (
    <View>
      <SiblingA count={count} onIncrement={() => setCount(count + 1)} />
      <SiblingB count={count} />
    </View>
  );
}
APressing increment in SiblingA updates only the button label in SiblingA, SiblingB remains unchanged.
BPressing increment in SiblingA updates only SiblingB text, button label stays the same.
CPressing increment in SiblingA updates count in both SiblingA button label and SiblingB text.
DPressing increment in SiblingA does not update any displayed count.
Attempts:
2 left
💡 Hint

Think about where the state is stored and which components receive it as props.

🧠 Conceptual
intermediate
1:30remaining
Why do we lift state up in React Native?

What is the main reason to lift state up to a common parent component in React Native?

ATo share state between sibling components that need to stay in sync.
BTo make each component manage its own state independently.
CTo avoid passing props between components.
DTo improve the app's performance by reducing renders.
Attempts:
2 left
💡 Hint

Think about components that need to show the same data or react to the same changes.

lifecycle
advanced
1:30remaining
What happens to child components when lifted state changes?

In React Native, when a parent component's state changes due to lifting state up, what happens to its child components?

AAll child components re-render with updated props reflecting the new state.
BOnly the child component that triggered the state change re-renders.
CChild components do not re-render unless their own state changes.
DThe parent re-renders but child components remain unchanged.
Attempts:
2 left
💡 Hint

Remember how React updates components when props change.

🔧 Debug
advanced
2:00remaining
Why doesn't the child's useEffect log count changes?

Given this React Native code snippet, why does Child's console.log not show updates to count when the button in Parent is pressed?

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

function Parent() {
  const [count, setCount] = useState(0);
  return (
    <View>
      <Button title="Increment" onPress={() => setCount(count + 1)} />
      <Child count={count} />
    </View>
  );
}

function Child({ count }) {
  useEffect(() => {
    console.log('Count changed:', count);
  }, []);
  return <Text>{count}</Text>;
}
AThe Parent does not pass the count prop correctly to Child.
BThe Child's useEffect has an empty dependency array, so it does not react to count changes.
CThe setCount function is not updating the state.
DThe Child component is not rendered inside Parent.
Attempts:
2 left
💡 Hint

Look at the dependencies of the useEffect hook in Child.

navigation
expert
2:30remaining
How does lifting state up affect navigation state in React Native apps?

In a React Native app using React Navigation, if you lift state up to a parent component that wraps multiple screens, what is a key effect on navigation?

AThe lifted state resets every time you navigate to a new screen.
BNavigation becomes slower because state is duplicated in each screen.
CYou cannot lift state up when using React Navigation.
DThe lifted state persists across screen navigations, allowing shared data between screens.
Attempts:
2 left
💡 Hint

Think about where the state lives relative to the navigation container.