The event listeners need to be removed due to following reason.
- Avoid memory leaks, if the browser is not handled it properly.Modern browsers will garbage collect event handlers of removed DOM elements but it is not true in cases of legacy browses like IE which will create memory leaks.
- Avoid collisions of events of components.
- Remove the side effects when the reference of event listeners are stored in some persistence such as local storage or some thing like this
Here is a good article to get an insights on event listners
If event listeners are not removed, it will cause memory leak for older browsers (IE 6 and 7, if i recall correctly). Hence will cause unnecessarily high memory utilization which will lead to many problems. Also, in this case, you will have issues with debugging as in a big project you never have control of the entire codebase and you can have multiple references of same component loaded from different components and if memory leak is not handled by the browser, then it will create a lot of confusion regarding the triggered events.
Modern browsers do remove event listeners on components when they are unmounted, however for some reason if you store the reference of this node in any other component that is not mounted or in localStorage, Garbage collector will not be able process this and it can potentially cause memory leaks.
Hence, the safest way to handle such scenarios is to manually remove event listeners in
P.S. With hooks, react provides way to return a function which can be used to remove listeners in the
- React why should I remove event listeners?
- Is it too expensive to add and remove event listeners on every call of my React custom hook? How to avoid it?
- Why should event listeners be removed in Reactjs?
- Why socket.io does not work further on react components after I remove all listeners in another component?
- Add and Remove event listeners in React with componentDidMount and componentWillUnmount
- Remove Event Listener On Unmount React
- Why React event handler is not called on dispatchEvent?
- Does webpack remove custom variables or event listeners attached to `window` in production?
- why react should usually be a prod dependency and not dev-dependency
- Why does a react onClick event append a question mark to my url?
- Should I use custom event delegation in React JS
- Why ajax request should be done in componentDidMount in React components?
- (ReactJS) Non-interactive elements should not be assigned mouse or keyboard event listeners
- React navigation event listeners being called multiple times
- why should we use import React from 'react'
- Remove event listener manually react hooks
- why should I not use CDN for react & babel?
- Why react server side rendering strips event handlers?
- How can you remove a Jquery Document Click Listeners Effecting React Element?
- MobX - Why should I use `observer` when I could use `inject` when injecting data into a React component
- React how to remove listners correctly in componentWillUnmount, why do I need the bind in the constructor?
- How to Attach Drag & Drop Event Listeners to a React component
- Converting HTMLElement to a React Element, and keeping any event listeners
- Using React Hooks, why are my event handlers firing with the incorrect state?
- Why should one use React with Meteor?
- React HOC—add Event Listeners to wrapped Component
- Why should you `extend` Component in React (Native)?
- React event listeners on array of elements
- Why react calls function in subcomponents event when this subsomponents not rendered?
- Why npm start is throwing events.js:187 throw er; // Unhandled 'error' event in my react project?
More Query from same tag
- How did i disable 1 tab on small screen in react js using ant design
- .babelrc being hidden in in MacOS Mojave update causing build fail
- Cannot edit todo item in todo list
- React is slow when rendering many elements
- How to defend routes in reactJS
- React router consume my url request to back-end API
- How Can I Setup `react-select` to work correctly with server-side data by using AsyncSelect?
- Building a unidirectional graph in React
- Radio react-bootstrap not invoking onClick event
- Where to save session id generated from the backend?
- Property ... does not exist on type 'IntrinsicAttributes & ...'
- React: create simple admin panel and get TypeError: headers is undefined
- Vertical align using MUI Paper
- Render sub-component error using React.js
- Focused elements display wrong style
- My React-Redux Code is Running Functions Before the Previous Functions can Finish
- React: Unique keys and optimistic updating
- How can i add React loading spinner when item is fetching from database?
- React: ChecBox checked state programmatically not reflecting checkbox state
- navigation is giving me blank page
- Unable to toggle state in react
- Error: Request failed with status code 405 , payload return HTML not JSON like intended
- How can new objects be placed in an array as values?
- How to build custom / dynamic routing in Next.js React Framework
- how do I fix my code in order to display one table?
- How to test componentDidUpdate()?
- React Checkbox Stays Checked Even After Component is Unmounted
- Need Direction on Iterating through an array
- How to type MUI Typography props? v5