I dont know what the following does

46 Views Asked by At

I don't understand what this code does

useEffect(() => {    
    document.addEventListener('input', event => {
      if (event.target.tagName.toLowerCase() !== 'textarea') {
        return
      }
      autoExpand(event.target)
    }, false)
  }, [])
1

There are 1 best solutions below

0
On BEST ANSWER

Let me try to explain what you have in your code.

About useEffect hook you can read in the documentation:

The Effect Hook lets you perform side effects in function components.

In your case the dependency array is empty [] which means it is called only once when the functional component loaded. Similarly like in class based component but a combined life-cycle events, from the docs:

If you’re familiar with React class lifecycle methods, you can think of useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined.

If you have an object in the dependency array, it will trigger again the passed function once it is changing.

I suggest to read further about useEffect hook in more details below:
https://reactjs.org/docs/hooks-effect.html

It will help you to understand deeper. I hope that helps!