0
rated 0 times
[
7]
[
7]
/ answers: 1 / hits: 7247
/ 3 Years ago, fri, may 7, 2021, 12:00:00
HELP. i use react hook, i got the error when i type the input.
error:
Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component.
Code:
const AddUser = () => {
const initialUserState = {
id: null,
name: '',
age: 0
}
const [users, setUsers] = useState(initialUserState)
const handleChange = (e) => {
setUsers({ [e.target.name]: e.target.value })
e.preventDefault()
}
return (
<div>
<input name="name" type="text" value={users.name} onChange={handleChange}/>
<input name="age" type="number" value={users.age} onChange={handleChange}/>
</div>
)}
More From » reactjs