When i define the hooks state in the parent function i lost input field focus on first key press. I need the state definition in the root function.
import React, { useState } from 'react'
function Test1(props) {
const [test, setTest] = useState({value1: "", value2:""});
const Test = () => {
const handleChange= (e) => {
const _test = {...test, [e.target.name]: e.target.value}
setTest(_test)
}
return (
<div style={{ margin: "200px" }}>
<input name="value1" value={test["value1"]} onChange={handleChange}></input>
<input name="value2" value={test["value2"]} onChange={handleChange}></input>
<button onClick={() => console.log(test)}>Console.Log</button>
</div>
)
}
return (
<Test />
);
}
export default Test1;
But if I move the state definition in to the child function it works.
import React, { useState } from 'react'
function Test1(props) {
const Test = () => {
const [test, setTest] = useState({value1: "", value2:""});
const handleChange= (e) => {
const _test = {...test, [e.target.name]: e.target.value}
setTest(_test)
}
return (
<div style={{ margin: "200px" }}>
<input name="value1" value={test["value1"]} onChange={handleChange}></input>
<input name="value2" value={test["value2"]} onChange={handleChange}></input>
<button onClick={() => console.log(test)}>Console.Log</button>
</div>
)
}
return (
<Test />
);
}
export default Test1;
So! Why is this happening and how can I get over it?