@deepdub/react-arborist
Version:
15 lines (14 loc) • 526 B
JavaScript
import { useMemo } from "react";
import { useTreeApi } from "../context";
export function useFreshNode(index) {
const tree = useTreeApi();
const original = tree.at(index);
if (!original)
throw new Error(`Could not find node for index: ${index}`);
return useMemo(() => {
const fresh = original.clone();
tree.visibleNodes[index] = fresh; // sneaky
return fresh;
// Return a fresh instance if the state values change
}, [...Object.values(original.state), original]);
}