Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
Using Custom Hook with search parameters

next it's a code that uses a HOC for showing some components while a fetch (with a custom hook) to an API REST is made.

From useSearchParams() I get the query string that will be used in fetching data.

The issue here is that when the component is rendered useFetch is executed but the search parameter (param) is not ready, then fetch is executed with null value.

const withLoading = (WrappedComponent) => {
    const HocComponent = ({ ...props }) => {
        const [urlParams, setUrlParams] = useSearchParams()
        const [param, setParam] = useState()
        useEffect(() => {
            console.log(urlParams.get("text"));
            setParam(urlParams.get("text"))
        }, [urlParams])
        const { data, loading, error } = useFetch("api_server", "/articles", { 'text': param })
        if (loading) return (
            <div className="grid grid-cols-6">
                {Array(6).map(el => <ProductPulse />)}
            </div>
        )
        return <WrappedComponent data={data} error={error} />
    }
    return HocComponent
}

Any comments on how to solve this issue using the hook useFetch?

I've tried many different ways without any luck.

I'm specting to use the custom hook useFetch in this scenario.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your API has been called when HocComponent gets rendered, so it won't wait for your API response.

Ideally, you should have loading state to wait for the fetch completed, and you also can set data state instead of param state in useEffect whenever urlParams has changed.

const withLoading = (WrappedComponent) => {
    const HocComponent = ({ ...props }) => {
        const [urlParams, setUrlParams] = useSearchParams()
        //`response` state to listen to API response changes
        const [response, setResponse] = useState({ data: null, error: null, loading: true })
        useEffect(() => {
            console.log(urlParams.get("text"));
            const param = urlParams.get("text");
            //`urlParams` gets changed and not undefined, we call API here
            if(urlParams.get("text")) {
               const { data, loading, error } = useFetch("api_server", "/articles", { 'text': param });
               setResponse({ data, loading, error })
            }

        }, [urlParams])

        const { loading, data, error } = response
        
        if (loading) return (
            <div className="grid grid-cols-6">
                {Array(6).map(el => <ProductPulse />)}
            </div>
        )
        return <WrappedComponent data={data} error={error} />
    }
    return HocComponent
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

The issue with the current implementation is that you are caching the text queryString parameter in state, which delays the param state value being available by at least one render cycle.

The fix is trivial though, just consume the text queryString parameter directly, there's no need for any param state.

Example:

const withLoading = Component => props => {
  const [urlParams] = useSearchParams();

  const param = urlParams.get("text");

  const { data, loading, error } = useFetch(
    "api_server",
    "/articles",
    { text: param },
  );

  if (loading) {
    return (
      <div className="grid grid-cols-6">
        {Array(6).map((el, key) => <ProductPulse key={key} />)}
      </div>
    );
  }

  return <Component data={data} error={error} />;
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda