Let’s create an AddEdit with same component

Today we are going to perform a crud app where we will add and edit the form with same component .
We will use react-router-dom , formik and yup. so let’s dive to the code.

Create a component as AddEdit.js

import React, { useEffect, useState } from…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Rajiv Chaulagain

Today we are going to perform a crud app where we will add and edit the form with same component .
We will use react-router-dom , formik and yup. so let's dive to the code.

Create a component as AddEdit.js

import React, { useEffect, useState } from 'react'
import { Formik, Form } from 'formik'
import { useMutation, useQuery, useQueryClient } from 'react-query'


const AddEdit = () => {

  const history = useHistory()

  const user = useUser()

  const params: any = useParams()

  const queryClient = useQueryClient()

  const id: number = Number(params?.id)

  const isAddMode = !id

  const { data, isLoading, isError } = useQuery(['data', id], () => getData(id), {
    enabled: !!id,
  })

  const onFormSubmit = (values: any) => {
    isAddMode ? addFnc : editFnc
  }

  const initValues = {
    name: isAddMode ? '' : data?.name
  }

  return (
    <>
      <div className=''>
        <Formik validationSchema={Schema} initialValues= {initValues} onSubmit={onFormSubmit}>
          {(props) => (
            <Form className='w-100 pb-10'>
              <Field name="name" />
              <button
                type='submit'>
                  'Submit'
              </button>
            </Form>
          )}
        </Formik>
      </div>
    </>
  )
}

export default AddEdit

So here , we created a function and hence we added the code.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Rajiv Chaulagain


Print Share Comment Cite Upload Translate Updates
APA

Rajiv Chaulagain | Sciencx (2022-10-14T15:59:45+00:00) Let’s create an AddEdit with same component. Retrieved from https://www.scien.cx/2022/10/14/lets-create-an-addedit-with-same-component/

MLA
" » Let’s create an AddEdit with same component." Rajiv Chaulagain | Sciencx - Friday October 14, 2022, https://www.scien.cx/2022/10/14/lets-create-an-addedit-with-same-component/
HARVARD
Rajiv Chaulagain | Sciencx Friday October 14, 2022 » Let’s create an AddEdit with same component., viewed ,<https://www.scien.cx/2022/10/14/lets-create-an-addedit-with-same-component/>
VANCOUVER
Rajiv Chaulagain | Sciencx - » Let’s create an AddEdit with same component. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/14/lets-create-an-addedit-with-same-component/
CHICAGO
" » Let’s create an AddEdit with same component." Rajiv Chaulagain | Sciencx - Accessed . https://www.scien.cx/2022/10/14/lets-create-an-addedit-with-same-component/
IEEE
" » Let’s create an AddEdit with same component." Rajiv Chaulagain | Sciencx [Online]. Available: https://www.scien.cx/2022/10/14/lets-create-an-addedit-with-same-component/. [Accessed: ]
rf:citation
» Let’s create an AddEdit with same component | Rajiv Chaulagain | Sciencx | https://www.scien.cx/2022/10/14/lets-create-an-addedit-with-same-component/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.