Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
How to clear input field and reset select option after creating data in react

I am trying to create a form with some fields. but I don't know how to clear my input field after inserting data and I would also like my select option to go back to the default "Select option".

const AddBook = () => {
const [authors, setAuthors] = useState([]);
const [book, setBook] = useState([]);
const { loading, data } = useQuery(getAuthorsQuery)
const [addBook] = useMutation(addBookMutation)

const handleSubmit = (e) => {
    e.preventDefault();
    addBook({
        variables: {
            name: book.name,
            genre: book.genre,
            authorId: book.authorId    
        },
        refetchQueries:[{query:getBooksQuery}]
    })
    
}

const handleChange = (e) => {
   
   setBook({...book,[e.target.name]: e.target.value})
}

useEffect(() => {
    if (!loading) {
        setAuthors(data.authors)
    }
}, [data, loading]);
return (
    <form id="add-book" onSubmit={handleSubmit}>
        <div className='field'>
            <label>Book name</label>
            <input type="text" name="name" onChange={handleChange}/>
        </div>
        <div className='field'>
            <label>Genre</label>
            <input type="text" name="genre" onChange={handleChange}/>
        </div>
        <div className='field'>
            <label>Author</label>
            <select name="authorId" onChange={handleChange}>
                <option>Select Option</option>
                {authors.map(author => <option name="authorId" key={author.id} value={author.id}>{author.name}</option>)}
            </select>
        </div>
        <button>Add Book</button>
    </form>

)

};

export default AddBook

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Solution

        import { useMutation, useQuery } from '@apollo/client'
    import React, { useEffect, useRef, useState } from 'react'
    import { addBookMutation, getAuthorsQuery, getBooksQuery } from '../queries/queries';



const AddBook = () => {
    const initalState = {
        name: "",
        genre:""
    }
    const [authors, setAuthors] = useState([]);
    const [book, setBook] = useState(initalState);
    const { loading, data } = useQuery(getAuthorsQuery)
    const [addBook] = useMutation(addBookMutation)
    **const selectElement = useRef(null)**

    const handleSubmit = (e) => {
        e.preventDefault();
        addBook({
            variables: {
                name: book.name,
                genre: book.genre,
                authorId: book.authorId    
            },
            refetchQueries:[{query:getBooksQuery}]
        })
        **setBook(initalState)
        selectElement.current.value = ""**
   
    }

    const handleChange = (e) => {     
       setBook({...book,[e.target.name]: e.target.value})
    }

    useEffect(() => {
        if (!loading) {
            setAuthors(data.authors)
        }
    }, [data, loading]);
    return (
        <form id="add-book" onSubmit={handleSubmit}>
            <div className='field'>
                <label>Book name</label>
                <input type="text" name="name" value={book.name} onChange={handleChange}/>
            </div>
            <div className='field'>
                <label>Genre</label>
                <input type="text" name="genre" value={book.genre} onChange={handleChange}/>
            </div>
            <div className='field'>
                <label>Author</label>
                **<select ref={selectElement} name="authorId" onChange={handleChange}>
                    <option value="">Select Option</option>
                    {authors.map(author => <option name="authorId" key={author.id} value={author.id}>{author.name}</option>)}
                </select>**
            </div>
            <button>Add Book</button>
        </form>

    )
};

export default AddBook
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!