0

I am working on a program which fetches a SVG and converts it into a set of React components with some interactivity.

The problem I am having is the onClick handler that I register when building the tree is not getting the updated version of my state variables.

In my buildTree function, I have the following code:

⋮
if(["key"].includes(attrs.className)) {
  setKeys((ks) => [...ks, attrs.ref])
  attrs.onClick = (evt) => clickShow(evt.target, attrs.ref.current)
}

return React.createElement(root.nodeName, attrs, children)

When I click, however, the keys variable an empty array.

This pen demonstrates the issue. The key is the little picture of a house in the lower left-hand corner. Clicking on a floor should change the view, but as you can see in the console, keys is an empty array.

dysbulic
  • 3,005
  • 2
  • 28
  • 48

1 Answers1

0

I found this answer which explains the scoping issue I'm having and that I can use useRef to get a properly mutable variable.

dysbulic
  • 3,005
  • 2
  • 28
  • 48