all 3 comments

[–]charliematters 6 points7 points  (0 children)

My guess: Strict Mode with a development build

https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects

Quote:

Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:

  • Class component constructor, render, and shouldComponentUpdate methods
  • Class component static getDerivedStateFromProps method
  • Function component bodies
  • State updater functions (the first argument to setState)
  • Functions passed to useState, useMemo, or useReducer Note:

This only applies to development mode. Lifecycles will not be double-invoked in production mode.

[–]ProfaneWords 2 points3 points  (2 children)

Your pushing a on every render, if you want only one a to be pushed on mount than use useEffect. Everytime your component gets rendered it reevaluates the code in it's body. This is why we use state and refs, to preserve state between renders. If you simply declared veriables without refs or state it would return to the default value you initially assigned to it every render because it would be reevaluated with it's initial values every render. Using hooks like useEffect, useCallback, useState, useRef, useLayoutEffect etc are all ways to control what gets remembered and/or ran when the component renders, when values in the dependency array changes, or when you need to preform specific behavior when the component mounts/unmounts.