-
-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
examples using hooks / useState ? #68
Comments
I don't think I had this problem in my application using 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 |
@williaster your code works well for me! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: