Skip to content
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

handler is still fired after unmount #11

Closed
hartatovich opened this issue Oct 27, 2019 · 4 comments
Closed

handler is still fired after unmount #11

hartatovich opened this issue Oct 27, 2019 · 4 comments

Comments

@hartatovich
Copy link

hartatovich commented Oct 27, 2019

unmounting the Event component wont unsubscribe the handler for the eventname

`
function NotificationList({}) {

const onMessage = (action) => {

    console.log("got new notification from socket", action);

};

return (
    <>
        {/*list component goes here*/}
        <Event event='notification' handler={onMessage}/>
    </>
)

}
`

"react": "^16.10.2"
"react-socket-io": "^0.2.5"

@charleslxh
Copy link
Owner

Im my memory, event will unsubscribe when component unmount!

@bojan88
Copy link

bojan88 commented Feb 27, 2020

The problem here is when we have an update before unmount. If the handler function is changed (prop updated with different function reference), socket.off is not going to remove it.

@bojan88
Copy link

bojan88 commented Mar 4, 2020

@hartatovich since you are using hooks, you can do this:

const onMessage = React.useCallback((action) => {
    console.log("got new notification from socket", action);
}, []);

It should fix your issue. It's basically what Alexey proposed, but with hooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@bojan88 @hartatovich @charleslxh and others