0

How do I clear the input after button submit? The text I input is still there after I submit.

Many thanks in advance and greatly appreciated. Thanks again

const [value, setValue] = useState("");

 const handleInput = e => {
        setValue(e.target.value);
      };

    const onSubmit = (e) => {
        e.preventDefault();
        setValue("");
      }
    

    {
 dbdata.map(item => {
                    return(
                        <div className="card mb-5" key={item._id} style={{maxWidth:"70%",height:"70%"}} >
                            <img src={item.photo} className="card-img-top" alt="..." style={{maxWidth:"100%",height:"100%"}} />
                            <div className="card-body" >
                                <h6 className="card-text">{item.reviews.length} Reviews
                                <form className="input-group mb-3" onSubmit={onSubmit}>
                                    <input type="text" className="form-control" placeholder="add a post onChange={handleInput}  value={value} />
                                    <div class="input-group-append">
                                        <button type="submit" class="btn btn-primary" onClick={(e) => {
                                          makePost(value, item._id)}} type="button">Post</button>
                                    </div>                    
                                </form>
                            </div>
                        </div>

                    )
                })
}

1 Answer 1

2

You can try to add a form tag with a onSubmit function, which inside sets the value as an empty string after preventing the default behavior of submitting a form

Edit: I see you have a makePost function, I think you can move the code of this function inside of my onSubmit one or putting my code inside of your makePost function

const [value, setValue] = useState("");

 const handleInput = e => {
        setValue(e.target.value);
      };

  const onSubmit = (e) => {
    e.preventDefault();
    setValue("");
  }

<div className="input-group mb-3">
<form onSubmit={onSubmit}
   <input type="text" className="form-control" placeholder="add a post" onChange={handleInput} value={value} /></form>
        <div class="input-group-append">
          <button type="submit" class="btn btn-primary" onClick={(e) => {makePost(value, item._id)}} type="button">Post</button>
        </div>                    
</div>
Sign up to request clarification or add additional context in comments.

9 Comments

it didn't get reset or clear the input after post
Oh I just saw that the value is not binded to your input, is that normal ? You should have an attribute value with the value in your input tag, like : <input value={value} />
I think you should copy paste your whole component because some stuff is missing
No worries, after I added the value={value}, all input has the same values. It is because I am iterating using data.map, so therefore there are many inputs. It is for reviewing products. Thanks
is your whole component very big ? Copy paste the whole thing if you can, then it will help us to see everything
|

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.