Challenge - 5 Problems
Custom Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this custom button component?
Consider this React Native custom button component. What will you see on the screen when it is rendered?
React Native
import React from 'react'; import { TouchableOpacity, Text } from 'react-native'; const MyButton = ({ label }) => { return ( <TouchableOpacity accessibilityRole="button" style={{ backgroundColor: '#4CAF50', padding: 10, borderRadius: 5 }}> <Text style={{ color: 'white', fontSize: 16 }}>{label}</Text> </TouchableOpacity> ); }; export default function App() { return <MyButton label="Press Me" />; }
Attempts:
2 left
💡 Hint
Look at the styles and accessibilityRole used in TouchableOpacity.
✗ Incorrect
The component uses TouchableOpacity with a green background and white text. The accessibilityRole is set to 'button' making it accessible. So the output is a green rounded button with white text 'Press Me'.
❓ lifecycle
intermediate2:00remaining
What happens when the custom component's prop changes?
Given this React Native custom component that uses useEffect, what will happen when the prop 'count' changes?
React Native
import React, { useEffect } from 'react'; import { Text } from 'react-native'; const CounterDisplay = ({ count }) => { useEffect(() => { console.log(`Count changed to ${count}`); }, [count]); return <Text>Count is {count}</Text>; };
Attempts:
2 left
💡 Hint
Check the dependency array in useEffect.
✗ Incorrect
The useEffect hook runs its callback every time the 'count' prop changes, logging the new count. The text also updates to show the current count.
advanced
2:00remaining
Which option correctly passes navigation prop to a custom component?
In React Native with React Navigation, you want to navigate from a custom button component. Which option correctly allows navigation when the button is pressed?
React Native
import React from 'react'; import { Button } from 'react-native'; const NavButton = ({ navigation }) => { return <Button title="Go" onPress={() => navigation.navigate('Home')} />; };
Attempts:
2 left
💡 Hint
Custom components need navigation prop passed or useNavigation hook.
✗ Incorrect
The NavButton component expects navigation as a prop. Passing it from the parent screen and using it inside onPress is correct. Option A would work if useNavigation hook was used inside NavButton, but here it is not.
📝 Syntax
advanced2:00remaining
What error does this custom component code produce?
Look at this React Native custom component code. What error will it cause?
React Native
import React from 'react'; import { View, Text } from 'react-native'; const Greeting = (name) => { return <View><Text>Hello, {name}!</Text></View>; };
Attempts:
2 left
💡 Hint
Check how function parameters receive props in React components.
✗ Incorrect
No error is thrown, but it renders 'Hello, [object Object]!' because the 'name' parameter receives the entire props object, which stringifies to '[object Object]'. To correctly access the prop, destructure as ({ name }).
🔧 Debug
expert2:00remaining
Why does this custom component not update on state change?
This custom component uses React Native state but does not update UI when the button is pressed. Why?
React Native
import React, { useState } from 'react'; import { View, Button, Text } from 'react-native'; const Counter = () => { let count = 0; const increment = () => { count += 1; }; return ( <View> <Text>Count: {count}</Text> <Button title="Increment" onPress={increment} /> </View> ); };
Attempts:
2 left
💡 Hint
React only re-renders when state or props change, not local variables.
✗ Incorrect
The variable 'count' is a local variable and changing it does not trigger React to re-render the component. To update UI, 'count' must be a state variable using useState.