hi I am using the component which return input text. I wrote two child component so I have two independent instance with independent state ,props. when I click the submit it shows the data which i get from the state. I am using the callback function , i append the data as a object with different key 1 and 2.But i am not able to access that childid inside the setstate. Anyone please help me on this.
const [Pdata,setPdata]=useState("");
function callbackdata(childid,data){
setPdata((prev)=>({...prev,childid:data}))
}
function show()
{
console.log(Pdata)
}
return(
<div>
<Nav callbackfn={callbackdata} childid={"1"}></Nav>
<Nav callbackfn={callbackdata} childid={"2"}></Nav>
<button type='sumbit' onClick={show}>submit</button>
</div>
)
}
export default function Nav(props)
{
const [data,setdata]=useState("")
// console.log(props.childid)
function change(e)
{
setdata(e.target.value);
props.callbackfn(props.childid,data)
}
return(
<>
<input value={data} onChange={change}/>
</>
)
}