Calling a seperate function from within useEffect in react

  const [markers, setMarkers] =  React.useState([]);

  const mapRef = React.useRef();

  React.useEffect( () => {
  }, [markers])

  const fitBounds = () => {
    const bounds = new; => {

In the above code snippet im trying to resize the map to the fit the window in which all markers can be seen. I want this to occur everytime a new marker is added so I though a good place to put it would be in the useEffect() function. However I keep getting the Error: TypeError: Cannot read property ‘fitBounds’ of undefined. Im fairly new to react so if someone could explain to me how I can achieve this functionality Id really appreciate it.

49 thoughts on “Calling a seperate function from within useEffect in react”

Leave a Comment