react addeventlistener useeffect 1

react addeventlistener useeffect

//101-Example
const callback = useCallback((event: any) => {
      event.data.status === "complete" ? setProgress(0) : setProgress(event.data.uploadProgress);
    },[])
  
  useEffect(() => {
    window.addEventListener("file-upload", callback);
    return () => window.removeEventListener("file-upload", callback);
  }, [window, callback]);

Here is what the above code is Doing:
1. We create a callback function that will be called when the event is fired.
2. We use useEffect to add the event listener to the window object.
3. We return a function that will remove the event listener from the window object.

The useCallback hook is used to create a callback function that will not change unless one of the dependencies changes.

The useEffect hook is used to add and remove the event listener.

The useEffect hook takes a function as its first argument. This function will be called when the component is mounted and unmounted.

The second argument is an array of dependencies. If any of the dependencies change, the function will be called again.

In this case, we want the function to be called when the component is mounted and unmounted. So we pass an empty array as the second argument.

The function that is passed to useEffect returns a function that will be called when the component is unmounted. This function removes the event listener from the window object.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

The useEffect hook is also called when the component is updated. But we don’t want to add the event listener again. So we pass the window object as a dependency. This will ensure that the event listener is not added again.

Similar Posts