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 C
Quick Check:
Destructure with default = correct syntax [OK]
Hint: Use destructuring with = for default props [OK]
Common Mistakes:
Assigning default to entire props object
Confusing propTypes with defaultProps
Mutating props inside component
3. What will be displayed by this component if used as <Greeting /> without passing any props?
function Greeting({ name = 'Guest' }) {
return Hello, {name}!;
}
medium
A. Hello, !
B. Hello, Guest!
C. Hello, undefined!
D. Error: name is required
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 B
Quick Check:
Default prop used = 'Guest' [OK]
Hint: Default prop shows when no prop passed [OK]
Common Mistakes:
Expecting empty or undefined name
Thinking component throws error without prop
Ignoring default parameter syntax
4. Identify the error in this component that tries to set default props:
A. defaultProps is not supported on functional components
B. defaultProps should be set inside the function parameters
C. The component should use destructuring for props
D. There is no error; this is correct
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 D
Quick Check:
defaultProps on function = valid [OK]
Hint: defaultProps works on functions too [OK]
Common Mistakes:
Assuming defaultProps only works on classes
Thinking destructuring is mandatory
Confusing defaultProps with state
5. You want a component to show a default color 'blue' but only if the passed color prop is a non-empty string. Which is the best way to set this default in a React Native functional component?
hard
A. function Box({ color }) { color = color ? color : 'blue'; ... }
B. function Box({ color = 'blue' }) { ... }
C. function Box({ color }) { color &&= 'blue'; ... }
D. function Box({ color = '' }) { if (color) color = 'blue'; ... }
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's color &&= 'blue' sets 'blue' only if color is truthy (opposite). D's if (color) sets 'blue' only if truthy (wrong).
Final Answer:
function Box({ color }) { color = color ? color : 'blue'; ... } -> Option A