0
0
React Nativemobile~20 mins

Default props in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Props Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What will be displayed when no props are passed?
Consider this React Native component with default props. What text will appear if the component is rendered without any props?
React Native
import React from 'react';
import { Text } from 'react-native';

const Greeting = ({ name }) => {
  return <Text>Hello, {name}!</Text>;
};

Greeting.defaultProps = {
  name: 'Friend'
};

export default Greeting;
AComponent will crash
BHello, !
CHello, Friend!
DHello, undefined!
Attempts:
2 left
💡 Hint
Look at the defaultProps object and see what value is set for name.
📝 Syntax
intermediate
2:00remaining
Identify the correct way to set default props in a functional component
Which option correctly sets default props for a React Native functional component named Button?
React Native
import React from 'react';
import { Text, TouchableOpacity } from 'react-native';

const Button = ({ label }) => {
  return <TouchableOpacity><Text>{label}</Text></TouchableOpacity>;
};
AButton.defaultProps = function() { return { label: 'Click me' }; };
BButton.defaultProps = { label: 'Click me' };
CButton.defaultProps = ['Click me'];
DButton.defaultProps = label => 'Click me';
Attempts:
2 left
💡 Hint
defaultProps should be an object with keys matching prop names.
lifecycle
advanced
2:00remaining
When are default props applied in React Native functional components?
At what point in the component lifecycle are default props applied to a React Native functional component?
ABefore the component function runs, default props fill in missing props.
BAfter the component renders, default props replace undefined props.
CDuring the component unmount phase, default props are applied.
DDefault props are never applied automatically; you must check manually.
Attempts:
2 left
💡 Hint
Think about when the component receives its props to use inside the function.
🔧 Debug
advanced
2:00remaining
Why does this component show 'undefined' instead of the default prop?
This React Native component tries to use default props but shows 'undefined' when no prop is passed. Why?
React Native
import React from 'react';
import { Text } from 'react-native';

const Welcome = ({ user }) => {
  return <Text>Welcome, {user}!</Text>;
};

// Incorrect default props
Welcome.defaultProps = { user: 'Guest' };

export default Welcome;
AdefaultProps must be an object, not a function. Using a function causes 'user' to be undefined.
BThe prop 'user' is required and cannot have a default.
CThe component forgot to import React, so defaultProps don't work.
DdefaultProps only work with class components, not functional ones.
Attempts:
2 left
💡 Hint
Check the type assigned to defaultProps.
🧠 Conceptual
expert
2:00remaining
How do default props interact with destructured parameters with default values?
Given this React Native component, what will be the output if rendered with no props?
React Native
import React from 'react';
import { Text } from 'react-native';

const Info = ({ message = 'Hello' }) => {
  return <Text>{message}</Text>;
};

Info.defaultProps = {
  message: 'Hi'
};

export default Info;
AComponent crashes
BHello
Cundefined
DHi
Attempts:
2 left
💡 Hint
defaultProps are applied to the props object before destructuring happens inside the component function.