I'm trying to use the useParams hook to grab a value from the route and use it in an async handler function for my page which also uses useEffect and useState.
I noticed that my page works fine until I reference the value (pageId) that I got from useParams. As soon as you reference placeId within fetchPlaces you start getting this error:
React Hook useEffect has a missing dependency: 'fetchPlace'
I do not understand what's going on--why does it fail as soon as I reference the value set by the useParams hook?
import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
function PlaceItem() {
const { placeId } = useParams();
const [place, setPlace] = useState([]);
useEffect(() => {
fetchPlace();
}, []);
async function fetchPlace() {
/*** IF YOU DO NOT REFERENCE placeID IT WORKS ***/
const placeData = await API.graphql({
query: queries.getPlaceDetails,
variables: { id: placeId },
});
setPlace(placeData);
}
return <Cards places={place} columns="1" />;
}
export default PlaceItem;