My useEffect hook dont seem to be running the block of code within its scope. Not exactly sure why, when i debug it the useEffect hook gets hit but the code within it never runs. I have placed a break point and it never lands on it.
import {useState, useEffect} from "react"; const useApiResult = (request) => { const [results, setResults] = useState(null); const [error, setError] = useState(null); useEffect(() => { loadData(request); }, [request]); const loadData = async (request) => { console.log(request) try { const response = await fetch(request); if(response.ok) { setResults(await response.json()); setError(null); } else { setError(await response.text()); } } catch (err) { setError(err); } } return [results, error]; }; export default useApiResult; Where I call useApiResult
import { useMemo } from 'react'; import { getPokemons } from '../requests'; import useApiResult from '../customHooks/useApiResults'; const usePokemons = () => { const request = useMemo(() => getPokemons(), []); return useApiResult(request); } export default usePokemons; getpokemons.js
export const BASE_URL = "https://pokeapi.co/api/v2"; const createUrl = (base, path) => `${base}${path}`; export const getPokemons = () => [ createUrl(BASE_URL, "/pokemon?offset=300&limit=10"), { method: "GET", } ]; https://stackoverflow.com/questions/65571882/useeffect-hook-wont-run-codeblock January 05, 2021 at 09:03AM


没有评论:
发表评论