Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Nextjs is broken cant useState or forms

im new to react so maybe i missed something. I am trying to create simple form for registration but seems like I cannot change the state.

`

import {useState} from 'react'


export const [inputs, setInputs] = useState({})

export  const handleChange = (event: any) => {
    const name = event.target.name
    const value = event.target.value
    setInputs(values => ({...values, [name]: value}))
  }

export  const handleSubmit = (event: any) => {
    event.preventDefault()
    console.log(inputs)
  }

type Props = {
  label: string
  placeholder?: string
  onChange: () => void
  name?: string
  value?:  any
}

export const TextField = ({ label, onChange, placeholder, name, value }: Props) => {
  return (
    <div style={{width:'100%'}}>
      <div className='text-sm my-2'>{label}</div>
      <input
        className='border-blue-200 border-2 rounded focus:ring-blue-200 focus:border-blue-200 focus:outline-none px-2'
        style={{ height: '45px', maxWidth: '280px', width: '100%', backgroundColor: '#0D0D0D' }}
        placeholder={placeholder}
        onChange={onChange}
        name={name}
        value={value}
      />
    </div>
  )
}

`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and in register file i have

`

<form action="" method="post">
          <div className='flex flex-col flex-wrap gap-y-5'>
            
              <TextField  name="email" value={inputs.email || ""} label='Enter Your email address:' onChange={() => {handleChange}} />
              <TextField name="password" value={inputs.password || ""} label='Enter Your Password:' onChange={() => {handleChange}} />
           
          </div>

          <div className='mt-7 flex items-center gap-x-2'>
            <PrimaryButton text='Register' onClick={() => {handleSubmit}} /> 
            <div className='text-sm'>
              Already Registered? <a onClick={() => push('/login')}>Login</a>
            </div>
          </div>
          </form>

`

i am getting this error

state broke

I was just trying to get the user input from email/pass and post to postgresql db but it say cannot read state

using @sachila-ranawaka answer produces this error

why

>Solution :

move the content inside the TextField component and exclude the export s

type Props = {
  label: string
  placeholder?: string
  onChange: () => void
  name?: string
  value?:  any
}

export const TextField = ({ label, onChange, placeholder, name, value }: Props) => {

   const [inputs, setInputs] = useState({})

   const handleChange = (event: any) => {
     const name = event.target.name
     const value = event.target.value
     setInputs(values => ({...values, [name]: value}))
   }

   const handleSubmit = (event: any) => {
     event.preventDefault()
     console.log(inputs)
   }

   return (
    <div style={{width:'100%'}}>

      ..........
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading