react-cytoscapejs icon indicating copy to clipboard operation
react-cytoscapejs copied to clipboard

How do i access cy in a function based component instead of a class based component as show in the example?

Open SaiMun92 opened this issue 4 years ago • 4 comments

SaiMun92 avatar Jun 21 '20 06:06 SaiMun92

I'm using the a React.useRef hook for that:

import React, { useRef, useCallback} from 'react';
import CytoscapeComponent from 'react-cytoscapejs';
import type cytoscape from 'cytoscape';

export interface GraphProps {
  className?: string;
}

export default function Graph(props: GraphProps) {
  const cy = useRef<cytoscape.Core | null>(null);
  const setCytoscape = useCallback(
    (ref: cytoscape.Core) => {
      cy.current = ref;
    },
    [cy],
  );
  return (
    <CytoscapeComponent
      cy={setCytoscape}
     ...
    />
  );
}


sgratzl avatar Jul 06 '20 12:07 sgratzl

@sgratzl Is there a way we can do this without hooks ?

knightswatch3 avatar Apr 27 '21 23:04 knightswatch3

@sgratzl Is there a way we can do this without hooks ?

what do you mean? the question of this issue is how to create an functional React component and React hooks are way to go for those

sgratzl avatar Apr 28 '21 11:04 sgratzl

I understand the question. But I am currently working in a project where I cannot use hooks. So, is there a solution where I am using class components and still can have the cy referred to the sibling components.

knightswatch3 avatar Apr 30 '21 18:04 knightswatch3