Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

My react app is adding a new item instead of updating and replacing the item. Instead of replacing the item it adds another new item underneath it.

The code:

import Button from "./Button" import Input from "./Input" import { useForm } from 'react-hook-form' import { server_calls } from '../api/server' import { useDispatch, useStore } from 'react-redux'; import { chooseISBN, chooseAuthor, chooseTitle, chooseLength, chooseCover } from "../redux/slices/RootSlice"; interface ContactFormProps { id?: string[] } const ContactForm = (props:ContactFormProps) => { const { register, handleSubmit } = useForm({}) const dispatch = useDispatch(); const store = useStore(); const onSubmit = (data: any, event: any) => { console.log(`ID: ${typeof props.id}`); console.log(props.id) console.log(data) if (props.id && props.id.length > 0 ) { server_calls.update(props.id[0], data) console.log(`Updated: ${ data.name } ${ props.id }`) setTimeout(() => {window.location.reload()}, 1000); event.target.reset() } else { dispatch(chooseISBN(data.isbn_number)); dispatch(chooseAuthor(data.author_name)); dispatch(chooseTitle(data.book_title)); dispatch(chooseLength(data.book_length)); dispatch(chooseCover(data.hardcover_or_paperback)); server_calls.create(store.getState()) setTimeout( () => {window.location.reload()}, 1000); } } return ( <div> <form onSubmit={handleSubmit(onSubmit)}> <div> <label htmlFor="isbn_number">ISBN</label> <Input {...register('isbn_number')} name='isbn_number' placeholder="ISBN"/> </div> <div> <label htmlFor="author_name">Author</label> <Input {...register('author_name')} name='author_name' placeholder="Author"/> </div> <div> <label htmlFor="book_title">Title</label> <Input {...register('book_title')} name='book_title' placeholder="Title"/> </div> <div> <label htmlFor="book_length">Length</label> <Input {...register('book_length')} name='book_length' placeholder="Length"/> </div> <div> <label htmlFor="hardcover_or_paperback">Cover</label> <Input {...register('hardcover_or_paperback')} name='hardcover_or_paperback' placeholder="Cover"/> </div> <div className="flex p-1"> <Button className="flex justify-start m-3 bg-slate-300 p-2 rounded hover:bg-slate-800 text-white" > Submit </Button> </div> </form> </div> ) } export default ContactForm

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education