0

I am working in Reactjs and i am using nextjs,Right now i am trying to clear input field after click but not successful,Here is my code

<form onSubmit={handleSubmit}>
            <input
            type="text"
            name="name"
            id="name"
            placeholder="Type your email here"
            value={state.name}
            onChange={handleChange}
          />

      <button type="submit" className='btn-remindme'>Remind me </button></form>

And i tried with following code for clear input text

const [state, setState] = useState({
    name: "",
    job: ""
  });
const handleChange = (e) => {
    //alert(`So your name is `);
    const value = e.target.value;
    setState({
      ...state,
      [e.target.name]: value
    });
  };

  const handleSubmit = (e) => {
  $("#name").val("")
    e.preventDefault();
    const data = {
    name: state.name,
      email: state.job
  };
    
    axios.post('https://xxxxxxxxxxx/',data).then(function(response) {
    console.log(response.data);
});

2 Answers 2

1

Just remove it from the state

 const handleSubmit = (e) => {

    setState({
      ...state,
      name: ""
    });
   ....
  }
Sign up to request clarification or add additional context in comments.

Comments

1

You have used value={state.name}

So on submit just clear state.name

const handleSubmit = (e) => {
......
setState({...state,name:''});
..........
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.