Home > Back-end >  Clear input target
Clear input target

Time:07-13

I'm using an input to fetch data in my database, but I'd like the input to be cleared after data is submitted.

        <input placeholder='Title' onChange={(event) => {setTitle(event.target.value);}}/> 
        <button onClick={writeTitleToDatabase}>Update Title</button>

I tried to use setTitle('') but it doesn't work properlly. The variable title is set to null but input field still filled.

const writeTitleToDatabase = () => {
    update(ref(database, 'id/'   fetchIdInfo),{      
      title          
    })
      setTitle('');
};

CodePudding user response:

Add missing value attribute in the input tag

 <input placeholder='Title' value={title} onChange={(event) => {setTitle(event.target.value);}}/> 
    <button onClick={writeTitleToDatabase}>Update Title</button>
  • Related