Complete the code to set a default value for the name prop in a React functional component.
function Greeting({ name = [1] }) {
return <h1>Hello, {name}!</h1>;
}Setting name = "Guest" in the function parameter provides a default value if no name prop is passed.
Complete the code to assign a default prop value using the defaultProps property.
function Button(props) {
return <button>{props.label}</button>;
}
Button.[1] = {
label: "Click me"
};React components can have a defaultProps property to set default values for props.
Fix the error in this React component by completing the default prop assignment correctly.
const Welcome = ({ greeting }) => <h2>{greeting}</h2>;
Welcome.[1] = {
greeting: "Hello!"
};The correct property to assign default props is defaultProps. Others cause errors.
Fill both blanks to set default props and use them in a functional component.
function Card({ title = [1] }) {
return <div><h3>{title}</h3></div>;
}
Card.[2] = {
title: "Default Title"
};The default value in the parameter can be set to a string, and defaultProps sets defaults outside the function.
Fill all three blanks to create a component with default props and use them inside.
const Label = ({ text = [1], color = [2] }) => {
return <span style={{ color: [3] }}>{text}</span>;
};
Label.defaultProps = {
text: "Name",
color: "blue"
};Default values in parameters match the defaultProps. The style color uses the color prop.