I am new to react, trying to dynamically create nodes using ReactFlow based off of a JSON. Using the code below, I will save my index.tsx and push a button which calls onAdd function, and it will work (creates the expected nodes). However, once I refresh the browser, the button stops working.
function Flow() {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const [newNodes, setNewNodes] = useState([])
const handleClick = () => {
fetch('/get_nodes')
.then((r) => r.json())
.then(data => {
console.log(data)
setNewNodes(data)
})
}
const onAdd = useCallback(() => {
handleClick()
console.log(newNodes)
setNodes((nds) => nds.concat(newNodes));
}, [setNodes]);
I have not tried too much, I have been trying to google this problem for a bit and I don't know how to describe my issue other than listed above. This is likely more related to React than ReactFlow specific.