0
0
React Nativemobile~20 mins

Context API in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Context API Master
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 Context usage?

Consider this React Native code using Context API. What text will be displayed on the screen?

React Native
import React, { createContext, useContext } from 'react';
import { Text, View } from 'react-native';

const ThemeContext = createContext('light');

function DisplayTheme() {
  const theme = useContext(ThemeContext);
  return <Text>{`Theme is ${theme}`}</Text>;
}

export default function App() {
  return (
    <ThemeContext.Provider value="dark">
      <View>
        <DisplayTheme />
      </View>
    </ThemeContext.Provider>
  );
}
ATheme is light
BTheme is undefined
CTheme is dark
DError: Cannot read property 'Provider' of undefined
Attempts:
2 left
💡 Hint

Look at the value passed to ThemeContext.Provider and what useContext returns.

state_output
intermediate
2:00remaining
What is the value of count after clicking the button twice?

In this React Native component using Context API, what will be the value of count after pressing the button two times?

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

const CountContext = createContext();

function Counter() {
  const { count, setCount } = useContext(CountContext);
  return (
    <View>
      <Text>{`Count: ${count}`}</Text>
      <Button title="Increment" onPress={() => setCount(count + 1)} />
    </View>
  );
}

export default function App() {
  const [count, setCount] = useState(0);
  return (
    <CountContext.Provider value={{ count, setCount }}>
      <Counter />
    </CountContext.Provider>
  );
}
ACount: 1
BCount: 2
CCount: 0
DError: setCount is not a function
Attempts:
2 left
💡 Hint

Each button press increases count by 1. Starting from 0, two presses add 2.

📝 Syntax
advanced
2:00remaining
Which option correctly creates and uses a Context with default value?

Which code snippet correctly creates a React Native Context with a default value and consumes it in a component?

A
const UserContext = createContext();
function User() {
  const user = useContext();
  return &lt;Text&gt;{`User: ${user}`}&lt;/Text&gt;;
}
B
const UserContext = createContext('guest');
function User() {
  const user = useContext(UserContext.Provider);
  return &lt;Text&gt;{`User: ${user}`}&lt;/Text&gt;;
}
C
const UserContext = createContext('guest');
function User() {
  const user = UserContext.useContext();
  return &lt;Text&gt;{`User: ${user}`}&lt;/Text&gt;;
}
D
const UserContext = createContext('guest');
function User() {
  const user = useContext(UserContext);
  return &lt;Text&gt;{`User: ${user}`}&lt;/Text&gt;;
}
Attempts:
2 left
💡 Hint

Remember how to call useContext with the context object.

🔧 Debug
advanced
2:00remaining
What error does this Context usage cause?

What error will this React Native code produce when running?

React Native
import React, { createContext, useContext } from 'react';
import { Text, View } from 'react-native';

const DataContext = createContext();

function ShowData() {
  const data = useContext(DataContext);
  return <Text>{data.message}</Text>;
}

export default function App() {
  return (
    <View>
      <ShowData />
    </View>
  );
}
ATypeError: Cannot read property 'message' of undefined
BSyntaxError: Unexpected token
CReferenceError: DataContext is not defined
DNo error, displays empty text
Attempts:
2 left
💡 Hint

Check what value useContext(DataContext) returns when no Provider is used.

🧠 Conceptual
expert
2:00remaining
Why use Context API instead of prop drilling in React Native?

Which is the best explanation for why developers use Context API instead of passing props through many components?

AContext API allows sharing data globally without passing props through every level, reducing code complexity and improving maintainability.
BContext API automatically updates the UI without needing state or props, making components stateless.
CContext API replaces Redux and other state management libraries entirely for all apps.
DContext API forces components to re-render less often by caching props internally.
Attempts:
2 left
💡 Hint

Think about how data is passed in component trees and what problems prop drilling causes.