i want to make off method from socket.io client to remove all match running listeners from all react components Because currently i'm listening to a same event on two different component , both are in componentDidMount,
Board.js
socket.on('achievement', (data) => {
const updateAchievement= [data, ...this.state.getAchievements];
this.setState({
recentAchievements: this.state.recentAchievements.concat(data),
getAchievements: updateAchievement
});
});
Dashboard.js
socket.on('achievement', (data) => {
const updateAchievement= [data, ...this.state.getAchievements];
this.setState({
recentAchievements: this.state.recentAchievements.concat(data),
getAchievements: updateAchievement
});
});
Currently if the end-user turn off listening to the event while on DashBoard.js , the Board.js is still actively receiving data which i don't want. The board.js component is always on the view that's why i can see the data still coming.
How i unsubscribe
DashBoard.js
componentWillUnmount(){
socket.off("achievement");
}
Like i said above, the Board component is always in the view and i can still see data coming in.
How can i stop this?