What if your app never broke just because a prop was missing?
Why Default props in React Native? - Purpose & Use Cases
Imagine you build a button component in your app. Sometimes you forget to set its color or text. Without a plan, the button might look broken or confusing.
Manually checking every time if a prop is missing slows you down. You write extra code to handle missing values, which can cause bugs and make your code messy.
Default props let you set fallback values automatically. If a prop is missing, React Native uses the default you defined. This keeps your UI consistent and your code clean.
const Button = ({color, text}) => {
const btnColor = color ? color : 'blue';
const btnText = text ? text : 'Click me';
return <Text style={{color: btnColor}}>{btnText}</Text>;
}const Button = ({color = 'blue', text = 'Click me'}) => {
return <Text style={{color}}>{text}</Text>;
}Default props make your components reliable and easy to use, even when some inputs are missing.
Think of a profile picture component that shows a default avatar if no image URL is provided. Default props handle this smoothly without extra checks.
Default props provide automatic fallback values.
They reduce repetitive code and bugs.
They improve user experience by keeping UI consistent.
Practice
Solution
Step 1: Understand what happens when props are missing
If a prop is not passed, the component might break or behave unexpectedly.Step 2: Role of default props
Default props provide a fallback value so the component still works smoothly without explicit props.Final Answer:
To provide fallback values when no props are passed -> Option AQuick Check:
Default props = fallback values [OK]
- Thinking default props force prop passing
- Confusing default props with styling
- Assuming default props make components stateful
Solution
Step 1: Identify default prop syntax in function parameters
Using destructuring with default values like { title = 'Hello' } is the modern and correct way.Step 2: Check other options
function MyComponent(props = { title: 'Hello' }) { ... } wrongly assigns default to whole props object, C uses propTypes which is for validation not defaults, D mutates props which is bad practice.Final Answer:
function MyComponent({ title = 'Hello' }) { ... } -> Option CQuick Check:
Destructure with default = correct syntax [OK]
- Assigning default to entire props object
- Confusing propTypes with defaultProps
- Mutating props inside component
<Greeting /> without passing any props?function Greeting({ name = 'Guest' }) {
return Hello, {name}!;
}Solution
Step 1: Check default prop value
The parameter name has a default value 'Guest' if no prop is passed.Step 2: Analyze component output
Since no name prop is passed, it uses 'Guest' and renders 'Hello, Guest!'.Final Answer:
Hello, Guest! -> Option BQuick Check:
Default prop used = 'Guest' [OK]
- Expecting empty or undefined name
- Thinking component throws error without prop
- Ignoring default parameter syntax
function Button(props) {
return {props.label};
}
Button.defaultProps = {
label: 'Click me'
};Solution
Step 1: Check if defaultProps works on functional components
React Native supports defaultProps on functional components, so this is valid.Step 2: Verify syntax correctness
The syntax used is correct and will provide default label if none passed.Final Answer:
There is no error; this is correct -> Option DQuick Check:
defaultProps on function = valid [OK]
- Assuming defaultProps only works on classes
- Thinking destructuring is mandatory
- Confusing defaultProps with state
Solution
Step 1: Understand default parameter limits
Default parameters only apply if the prop is undefined, not empty string.Step 2: Choose a method that checks for non-empty string
Using a conditional (color ? color : 'blue') ensures empty strings get replaced by 'blue'.Step 3: Compare options
A fails if color is empty string (uses empty). C'scolor &&= 'blue'sets 'blue' only if color is truthy (opposite). D'sif (color)sets 'blue' only if truthy (wrong).Final Answer:
function Box({ color }) { color = color ? color : 'blue'; ... } -> Option AQuick Check:
Conditional check handles empty string default [OK]
- Relying only on default parameters for empty strings
- Using &&= which sets value when truthy
- Using if (color) which triggers on truthy
Ready to prove it?
Pass the test and this pattern moves unknown
