Skip to content

Commit 77af9f1

Browse files
tidy up the state function and add in comments
1 parent 2057cd1 commit 77af9f1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/ui/SunburstChart/SunburstChart.jsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,36 @@ function SunburstChart({
2525
const clickHandler = useRef(onClick)
2626
const hoverHandler = useRef(onHover)
2727

28+
// this state stores the root node of the sunburst chart
2829
const [root] = useState(() => {
30+
// go through the data and add `value` to each node
2931
const stack = [data]
30-
const result = { ...data, value: selectorHandler.current(data) }
3132
const nodeMap = new Map()
33+
34+
// create a new root node with the value of the root node
35+
const result = { ...data, value: selectorHandler.current(data) }
36+
// add the root node to the node map
3237
nodeMap.set(data, result)
3338

39+
// while there are nodes to process, pop the last node from the stack
3440
while (stack.length > 0) {
3541
const node = stack.pop()
3642
const currentNode = nodeMap.get(node)
3743

44+
// if the node has children, process them
3845
if (Array.isArray(node.children)) {
3946
currentNode.children = node.children.map((child) => {
40-
const newChild = { ...child, value: selectorHandler.current(child) }
47+
const newChild = structuredClone(child)
48+
Object.assign(newChild, { value: selectorHandler.current(child) })
49+
4150
nodeMap.set(child, newChild)
4251
stack.push(child)
4352
return newChild
4453
})
4554
}
4655
}
4756

57+
// partition the data and add the `current` property to each node
4858
return partitionFn(result).each((d) => (d.current = d))
4959
})
5060

0 commit comments

Comments
 (0)