Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set a default value for the title prop in a React Native component.
React Native
function Header({ title = [1] }) {
return <Text>{title}</Text>;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using null or undefined as default will render nothing.
Forgetting to use quotes around string default values.
✗ Incorrect
Setting a default value for the
title prop ensures the component shows "Welcome" if no title is passed.2fill in blank
mediumComplete the code to assign default props using the defaultProps property.
React Native
const Button = ({ label }) => <Text>{label}</Text>;
Button.[1] = {
label: [2]
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names like propsDefault.
Not quoting string default values.
✗ Incorrect
The
defaultProps property sets default values for props. Here, label defaults to "Click me".3fill in blank
hardFix the error in the code to properly set default props for a functional component.
React Native
function Label(props) {
return <Text>{props.text}</Text>;
}
Label.defaultProps = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using array syntax instead of object.
Assigning a string directly instead of an object.
✗ Incorrect
Default props must be an object with keys matching prop names and their default values.
4fill in blank
hardFill both blanks to set default props and use them in the component.
React Native
const Greeting = ({ name = [1] }) => {
return <Text>Hello, [2]!</Text>;
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the prop variable inside JSX.
Not setting a default value for the prop.
✗ Incorrect
The default value for
name is "Guest". The component uses the name prop inside the text.5fill in blank
hardFill all three blanks to define default props and use them in a React Native component.
React Native
function Info({ title = [1], subtitle = [2] }) {
return (
<View>
<Text>{ [3] }</Text>
<Text>{subtitle}</Text>
</View>
);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string literals instead of prop variables inside JSX.
Not setting default values for both props.
✗ Incorrect
Default props
title and subtitle are set to strings. The component displays title and subtitle correctly.