在反应中创建数据后如何清除输入字段并重置选择选项

发布于 2025-01-11 01:18:51 字数 1683 浏览 0 评论 0原文

我正在尝试创建一个包含某些字段的表单。但我不知道如何在插入数据后清除输入字段,我也希望我的选择选项返回到默认的“选择选项”。

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>

)

};

导出默认AddBook

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话已封尘 2025-01-18 01:18:51

解决方案

        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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文