@gravity-ui/graph
Version:
Modern graph editor component
20 lines (19 loc) • 750 B
JavaScript
import React, { createContext, useContext } from "react";
export const GraphContext = createContext(null);
/**
* Hook for getting Graph instance from context
*
* @throws {Error} If used outside of GraphCanvas
*/
export function useGraphContext() {
const context = useContext(GraphContext);
if (!context) {
throw new Error("useGraphContext must be used within a GraphCanvas component. " +
"Make sure your GraphLayer/GraphPortal components are children of GraphCanvas.");
}
return context;
}
export function GraphContextProvider({ graph, children }) {
const contextValue = React.useMemo(() => ({ graph }), [graph]);
return React.createElement(GraphContext.Provider, { value: contextValue }, children);
}