I have some issue trying to update the state of my react functional componenet. I have set my componenet with my hook state like that.
function Tree(){
const [data,setData] = useState({
id:1,
label:'Root',
children:[
],
styles:{
border:"1px solid green",
padding:"10px 20px",
position:"absolute",
left:"45%",
top:"20%",
},
content:{
}
})
function addNode(parentId,nodeData){
const newData = data;
addChild(newData,parentId,nodeData)
setData(newData);
}
function addChild(node,parentId,nodeData){
if(parentId == node.id){
node.children.push(nodeData);
}else{
//console.log("looking trough nodes")
setNewState(node.children,parentId,nodeData)
}
}
function deleteNode(nodeId){
}
console.log("rendering")
return (<>
<Node children={data.children} styles={data.styles} addNode={addNode}></Node>
</>)
}
I am passing up the addNode method to the Node component through props and in the node component just called it in a click event. Im also using a recursive function to get a new state value, there's no problem to create a new value but I'd called out the state function and it doesnt rendering the component. I don't know what may be the reason that the state doesnt got updated. If some react exeprienced cant help me to resolve this problem, I'll be very greatfully with it. The addNode function takes an id and the value to add. The addChild method find the node recursively and pushed the value into itself children key. pd: Sorry for my english if is not good.