Open dcsan opened 3 years ago
I don't think I had this problem in my application using useRef
with a couple of checks and cleanup.
function MyComponent() {
const cyRef = useRef<CytoscapeRef | null>();
// cleanup cytoscape listeners on unmount
useEffect(() => {
return () => {
if (cyRef.current) {
cyRef.current.removeAllListeners();
cyRef.current = null;
}
};
}, []);
const cyCallback = useCallback(
(cy: CytoscapeRef) => {
// this is called each render of the component, don't add more listeners
if (cyRef.current) return;
cyRef.current = cy;
cy.ready(...);
cy.on(...);
},
[...dependencies],
);
return <Cytoscape cy={cyCallback} {...} />;
}
hmm ok useRef
always feels a bit dirty to me like you're changing stuff that react wants to control, and it will bite you eventually...
@williaster your code works well for me!
I'm trying to create a functional component with this lib, and
useState
hooks, but having problems dealing with thecy
instance. I need to add events to the cy instance, eg tap. but this seems to create a memory leak - now each time the graph is rendered again, there's another 'graph' created. tapping will now send 3, 4, ... etc events.what's the best way to manage this? do i need to put some code in to try and remove the
cy.on(...)
event before the next render?Thanks!
code is like this below