The value of use State when put into the input value comes out to [Object, object] but in dev tools, it is shown empty.
import { useState } from 'react'
import {Link} from 'react-router-dom'
function Login() {
const [input, setInput] = useState({
email: ' ',
password: ' '
})
return (
<>
<form className="form-group">
<div className="input-group">
<label htmlFor="email">Email</label>
<input type="text" id= 'email' name = 'email' className="form-input" value={input} />
</div>
<div className="input-group">
<label htmlFor="password">Password</label>
<input type="text" id='password' name='password' className="form-input" value={input}/>
</div>
<p>Don't have an Account? <Link>SignUp</Link> </p>
<button className='btn'>LogIn</button>
</form>
</>
)
}
export default Login
I cant even use any function.
2
Answers
The value of use State when put into the input value comes out to [Object, object]
This is because you need to access the object correctly. Instead of value=
{input}
, you need to usevalue={input.email}
, and similarly for passwordvalue={input.password}
useState()
sets the default state, and you have specified the default state as an empty string ”, that’s the reason the initial value will always be empty. You can assign something to check likeFor getting the value of set as default in the input field you must call it by its key, not the whole object. Change
value = {input}
tovalue = {input.email}
andvalue={input.password}
.The fields will not update when you will try type inside the input fields, you would need to specify
onChange={someEventHandleFunction}
wheresomeEventHandleFunction
will map the values to the provided keys.Alternatively, you can also use two different
useState()
to handle email and password separately.