React Js Reset to initial State using hooks
useState hook. First, store the initial state in a constant. Then, use the useState hook to create a state variable and a setter function. To reset the state, call the setter function with the initial state constant.
Thanks for your feedback!
Your contributions will help us to improve service.
How can you reset a React component's state to its initial values using hooks?
This React JS code snippet demonstrates a functional component named "App." It uses the useState hook to manage a state object called "person." Initially set to an empty state, this object has properties like name, country, salary, email, department, and position. Two functions, updateState and resetState, are defined. updateState updates the state with specific values, while resetState resets it to the initial empty state. The component displays buttons to trigger these functions and renders the "person" state as JSON in a preformatted text block. When the buttons are clicked, the state is either updated or reset, showcasing dynamic state management with React hooks.
Output of React Js Reset to initial State using hooks
