To set default props for a component in React, you can define a defaultProps
object for the component. Here is an example:
class MyComponent extends React.Component {
render() {
return <div>{this.props.text}</div>;
}
}
MyComponent.defaultProps = {
text: "Hello, World!"
};
In the above example, we have defined a defaultProps
object for the MyComponent
class. If the text
prop is not provided when using the component, it will default to "Hello, World!".