Final answer:
To set a default value in React JS, initialize state with defaults using class components or the useState hook in functional components. For form elements, use defaultValue or defaultChecked props.
Step-by-step explanation:
To set a default value in React JS, you can initialize the state with default values when you are using class components, or use the useState hook with a default value in functional components. In addition to these methods, for form elements, you can set default values directly on the elements using the defaultValue or defaultChecked props for input elements.
Class Component Example:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
myValue: 'Default Value'
};
}
}
Functional Component Example:
function MyComponent() {
const [myValue, setMyValue] = useState('Default Value');
// rest of your component
}
When using defaultValue in a form input:
Learn more about Default Value in React JS