how to clear state in react hooks

const { useState } = React;

function signupUser() {
  return new Promise(resolve => {
    setTimeout(resolve, 1000);
  });
}

const initialState = {
  username: "",
  email: "",
  password: "",
  passwordConfirmation: ""
};

const Signup = () => {
  const [
    { username, email, password, passwordConfirmation },
    setState
  ] = useState(initialState);

  const clearState = () => {
    setState({ ...initialState });
  };

  const onChange = e => {
    const { name, value } = e.target;
    setState(prevState => ({ ...prevState, [name]: value }));
  };

  const handleSubmit = e => {
    e.preventDefault();
    signupUser().then(clearState);
  };

  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label>
          Username:
          <input value={username} name="username" onChange={onChange} />
        </label>
      </div>
      <div>
        <label>
          Email:
          <input value={email} name="email" onChange={onChange} />
        </label>
      </div>
      <div>
        <label>
          Password:
          <input
            value={password}
            name="password"
            type="password"
            onChange={onChange}
          />
        </label>
      </div>
      <div>
        <label>
          Confirm Password:
          <input
            value={passwordConfirmation}
            name="passwordConfirmation"
            type="password"
            onChange={onChange}
          />
        </label>
      </div>
      <button>Submit</button>
    </form>
  );
};

ReactDOM.render(<Signup />, document.getElementById("root"));

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to clear state value using useState how to clear a useState in react how to clear usestate in react clear state object hooks react how to reset state hook in react how to clear state in functional component how to clear redux state in react hooks clear state when I click anywehere react hooks clear a react usestate how to clear a state in react usestate useState react then can we clear state react usestate clear state react hooks reset all state how to clear the state on unmount hooks how to clear a state value in react native hook usestate clear state react useState hook clearing how to clear the use State how to clear the history state in react hooks reset state in react hooks react clear state hook before use react native clear usestate how to reset state in react hooks useeffect clear state how to clear usestate clear state react hooks react reset state to empty hooks clearing state isnt working react hooks reset state react hooks react clear usestate clear state react react hooks clear state react clear all state hooks react clear state hook how to clean state with hooks reset reacthook useState reser react hooks useref after submit react usestate variable reset in render reset usestate when screen updated how to reset to function with react hookd how to use initialState on hooks react native reset to initial state react on api success everytime I reset a webpage, states return back to initial states - react useState clears const usestate resets constant variable reset state hooks reset hoks stat array reset functional compponent to previous state react hooks. reset state react usestate reset react usestate return to initial state react reset state hooks use state set default state how to clear several state in functional component react react reset context reset all state react hooks how to reinitiate a hook how to clear input with hooks clear input react hooks reset state after each setstate react hooks reset state to initial state react hooks blank test field after set state in react hooks react functional component usState being set and reset automatically how to clear all states react hooks useState reset how to clear initial value of state with hooks react state location react reset hooks cleartimeout in react clear input field react hooks reset to initial state with hooks in react reset context api to initial state react context reset state clean up in react hooks react clear example react custom hook clear form clear form react hooks react hook clear all data in a state clear array in state react react reset context how to clear a state in react how to reset value with usestate if I call empty use state what type is default state in react reset usestate on return how to clear state in react hooks restore usestate variable to default js functional compnent clear state
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source