how to clear state in react hooks

Solutions on MaxInterview for how to clear state in react hooks by the best coders in the world

showing results for - "how to clear state in react hooks"
Montserrat
28 Jul 2018
1const { useState } = React;
2
3function signupUser() {
4  return new Promise(resolve => {
5    setTimeout(resolve, 1000);
6  });
7}
8
9const initialState = {
10  username: "",
11  email: "",
12  password: "",
13  passwordConfirmation: ""
14};
15
16const Signup = () => {
17  const [
18    { username, email, password, passwordConfirmation },
19    setState
20  ] = useState(initialState);
21
22  const clearState = () => {
23    setState({ ...initialState });
24  };
25
26  const onChange = e => {
27    const { name, value } = e.target;
28    setState(prevState => ({ ...prevState, [name]: value }));
29  };
30
31  const handleSubmit = e => {
32    e.preventDefault();
33    signupUser().then(clearState);
34  };
35
36  return (
37    <form onSubmit={handleSubmit}>
38      <div>
39        <label>
40          Username:
41          <input value={username} name="username" onChange={onChange} />
42        </label>
43      </div>
44      <div>
45        <label>
46          Email:
47          <input value={email} name="email" onChange={onChange} />
48        </label>
49      </div>
50      <div>
51        <label>
52          Password:
53          <input
54            value={password}
55            name="password"
56            type="password"
57            onChange={onChange}
58          />
59        </label>
60      </div>
61      <div>
62        <label>
63          Confirm Password:
64          <input
65            value={passwordConfirmation}
66            name="passwordConfirmation"
67            type="password"
68            onChange={onChange}
69          />
70        </label>
71      </div>
72      <button>Submit</button>
73    </form>
74  );
75};
76
77ReactDOM.render(<Signup />, document.getElementById("root"));
queries leading to this page
clean up in react hooksreact clear state hook before usereact clear all state hooksreser react hooks useref after submitreact custom hook clear formclear state of react usestatehow to clear state in react hooksclear state when i click anywehere react hooksusestate clears constreact usestate resetclear hooks reactreset all state react hooksusestate react then can we clear statereact reset state to empty hooksreact reset contexthow to use initialstate on hooks react nativereact usestate variable reset in renderreact reset contexthow to clear all states react hooksreset state in react hooksrestore usestate variable to default jsclear state react hooksclear a react usestatehow to clear usestateclearing state isnt working react hookshow to clear several state in functional component reactusestate clear statereact usestate clear statereact clear state hookreact hooks reset statehow to clean state with hooksreset context api to initial stateclear input react hookseverytime i reset a webpage 2c states return back to initial states reacthow to clear redux state in react hooksuse state set default statehow to clear state in functional componenthow to clear the state on unmount hooksclear state in hooksclear form react hookshow to clear input with hooksreact hooks clear statereset functional compponent to previous statereset to initial state react on api successhow to clear a state value in react native hookhow to reset usestate in reactclear hooksreset state after each setstate react hookshow to reset state in react hookshow to clear initial value of state with hooks reactreset reacthook usestateuseeffect clear statereact hooks reset statehow to reset state on react hookshow to clear the use statereset state to initial state react hooksreact functional component usstate being set and reset automaticallyreact clear usestatereact clearing usestate how to clear a state in reactclear state object hooks reactclear state reactreact context reset stateclear array in state reactreset state hooksusestate resets constant variablehow to clear a state in react usestatereact native clear usestatestate location react reset hooksreset usestate when screen updatedhow to reset state hook in reactfunctional compnent clear statehow to reinitiate a hookreset hoks stat arrayhow to reset to function with react hookdclear input field react hooksreact hooks reset all statereset usestate on returnhow to clear state value using usestatereact usestate return to initial statereact hook clear all data in a stateusestate resetreact clear examplehow to clear a usestate in reacthow to clear usestate in reactcleartimeout in reactclear state function reactreset state react hooksif i call empty use state what type is default state in reactblank test field after set state in react hooksreact usestate hook clearinghow to clear the history state in react hooksreset to initial state with hooks in reactreact reset state hookshow to reset value with usestatehow to clear state in react hooks