简体   繁体   中英

How to update value of an input with onChange in reactjs?

I have a problem with updating the value of the input in reactjs. I succeeded to get data from the database in the input, but I can't remove the text or add another text. to understand it better please take a look at the picture below it looks like this .

and this is the code.

  const editPost = async (id) => {
    const data = await axios.get(`http://localhost:3007/posts/byId/${id}`, {
      headers: { accessToken: localStorage.getItem("accessToken") },
    });
    setPostObject({
      postTitle: data.data.postTitle,
      postText: data.data.postText,
    });
  };

and this is how I set up my inputs

        <input
          value={postObject.postText}
          onChange={handleUpdating}
/>

any suggestions will appreciate. thank you

It's just necessary to set postText on handleUpdating (that, according to your comment, is empty). Something like:

const handleUpdating = (e) => { 
   setPostObject({
      postTitle: postObject.postTitle,
      postText: e.target.value,
   });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM