With React 18.3.1, I get this error when I attempt to prevent an infinite loop on a fetch request:
src\pages\ItemDetail.js
Line 16:8: React Hook useEffect has a missing dependency: 'id'.remove the dependency array react-hooks/exhaustive-deps
ove the dependency array react-hooks/exhaustive-deps
export default function ItemDetail({ cartItems, setCartitems }) {
const [product, setProduct] = useState(null);
const [qty, setQty] = useState(1);
const { id } = useParams();
useEffect(() => {
fetch(process.env.REACT_APP_URL + "/product/" + id)
.then(res => res.json())
.then(res => setProduct(res.product))
}, [])
idis an external (to the hook) dependency and should be added to the dependency array, e.g.useEffect(() => { /* code that references id */ }, [id]);. What infinite loop are you referring to though? I don't see anything that would cause a render loop here unless you somehow addedproductas a dependency which the effect updates. Please edit to clarify the exact problem you have and include a complete and accurate minimal reproducible example and the complete error message.