Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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"));
Comment

PREVIOUS NEXT
Code Example
Javascript :: if else java 
Javascript :: react import json 
Javascript :: javascript object to query string 
Javascript :: how to search for a voice channel within a guild using discord.js 
Javascript :: js array comprehension 
Javascript :: referenceerror document is not defined node js 
Javascript :: convert string to unicode javascript 
Javascript :: capacitor.ionicframework.com to apk 
Javascript :: how to find for lable in jquery 
Javascript :: axios post form data and json 
Javascript :: ok that is something 
Javascript :: node main 
Javascript :: jquery list all event listeners 
Javascript :: convert a string to object javascript 
Javascript :: check how many files in a folder js 
Javascript :: jquery check if element is hidden 
Javascript :: group all items with same name js 
Javascript :: string to json nodejs 
Javascript :: angular ng default scss 
Javascript :: disable javascript chrome 
Javascript :: make text lowercase javascript 
Javascript :: radio button group get value javascript 
Javascript :: Downward Triangle Star Pattern in JavaScript 
Javascript :: nodejs delete s3 folder 
Javascript :: javascript convert a number in string 
Javascript :: javascript change color every second 
Javascript :: string indexing in js 
Javascript :: empty function after it is run javascript 
Javascript :: mongodb push to index 
Javascript :: node js post method 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =