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 ( ISBN Author Title Length Cover Submit ) } export default ContactForm

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
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
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Windows
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education