i have two component App
and Child
.And whenever there's a change in the input field of the Child
component it should update the change in the App
component instantly.
But rather than updating the App
instantly it is one step behind.
here is the code :-
App component
function App() {
const [ChildData, setChildData] = useState("");
return (
<div>
<div>
<Child passdata={setChldData}/>
</div>
<div>
{ChildData.name}
</div>
</div>
)
}
Child component
function Child(props) {
const [Data, setData] = useState({ name:'' ,title:'' ,phone:'' });
const handleChange = (e)=>{
setData({...Data, [e.target.name] : e.target.value})
props.passdata(Data)
}
return (
<div>
<h2>Child Data</h2>
<input type="text" name='name' onChange={handleChange} placeholder={"Name"}/>
<input type="text" name='title' onChange={handleChange} placeholder={"Title"}/>
<input type="number" name='phone' onChange={handleChange} placeholder={"Phone number"}/>
</div>
)
}
Whenever there;s a change in the input field it is one step behind.
i want the App
component change simultaneously with the input from Child