0
0
React Nativemobile~20 mins

Custom components in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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" />;
}
AA plain text 'Press Me' with no button styling or accessibility.
BA red button with black text 'Press Me'.
CA green rounded button with white text 'Press Me' that is accessible as a button.
DAn error because TouchableOpacity is missing required props.
Attempts:
2 left
💡 Hint
Look at the styles and accessibilityRole used in TouchableOpacity.
lifecycle
intermediate
2: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>;
};
AConsole logs 'Count changed to X' every time the count prop changes and updates the displayed text.
BConsole logs only once when the component mounts and never again.
CConsole logs nothing and the text never updates.
DCauses an infinite loop of console logs.
Attempts:
2 left
💡 Hint
Check the dependency array in useEffect.
navigation
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')} />;
};
APass navigation prop from parent screen to NavButton and use it inside onPress.
BUse useNavigation hook inside NavButton without passing props.
CCall navigation.navigate directly without passing or importing navigation.
DUse window.location.href to navigate.
Attempts:
2 left
💡 Hint
Custom components need navigation prop passed or useNavigation hook.
📝 Syntax
advanced
2: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>;
};
AReferenceError because View is not imported.
BSyntaxError due to missing return statement.
CTypeError because 'name' is not destructured from props.
DNo error, renders 'Hello, [object Object]!'
Attempts:
2 left
💡 Hint
Check how function parameters receive props in React components.
🔧 Debug
expert
2: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>
  );
};
ABecause Button component requires a different prop name for onPress.
BBecause 'count' is a local variable, not state, so React does not re-render on change.
CBecause Text component cannot display numbers.
DBecause the increment function is not called on button press.
Attempts:
2 left
💡 Hint
React only re-renders when state or props change, not local variables.