Danf
Danf

Reputation: 1559

React Custom Hook set function returned is not a function

So, I built a custom hook to fetch data from an api. Here is the code:

export const useLambdaApi = () => {
  const [data, setData] = useState()
  const [isLoading, setIsLoading] = useState(false)

  useEffect(() => {
    const fetchData = async () => { ... }
    fetchData();
  },[isLoading]);

  return [data, setIsLoading];
}

And in the component I need the data I do:

export default function Comp (props) {
  const [data, setIsLoading] = useLambdaApi()

  useEffect(() => {
    const interval = setInterval(() => {
      setIsLoading(true)
      console.log(Date())
    }, 10000);
    return () => {
      window.clearInterval(interval); // clear the interval in the cleanup function
    };
  },[data]);
  return( ... )
}

But I get a TypeError: TypeError: setIsLoading is not a function

I know this must be something silly, but I am relatively new to React, so any feedback would be of much help.

Thanks.


EDIT:

To provide more context I added more code to my snipped of the component. I try to update the isLoading state from a setInterval. But I also did try from useEffect without the interval, and outside of useEffect...

This is the Stack trace:

PatientBoard.js:26 Uncaught TypeError: setIsLoading is not a function
    at PatientBoard.js:26
(anonymous) @ PatientBoard.js:26
setInterval (async)
(anonymous) @ PatientBoard.js:25
commitHookEffectList @ react-dom.development.js:21100
commitPassiveHookEffects @ react-dom.development.js:21133
callCallback @ react-dom.development.js:363
invokeGuardedCallbackDev @ react-dom.development.js:412
invokeGuardedCallback @ react-dom.development.js:466
flushPassiveEffectsImpl @ react-dom.development.js:24223
unstable_runWithPriority @ scheduler.development.js:676
runWithPriority$2 @ react-dom.development.js:11855
flushPassiveEffects @ react-dom.development.js:24194
(anonymous) @ react-dom.development.js:23755
scheduler_flushTaskAtPriority_Normal @ scheduler.development.js:451
flushTask @ scheduler.development.js:504
flushWork @ scheduler.development.js:637
performWorkUntilDeadline @ scheduler.development.js:238

Upvotes: 4

Views: 7626

Answers (1)

awran5
awran5

Reputation: 4536

Use it like so:

Updated: Trigger re-fetching based on URL changes:

import React, { useEffect, useState } from "react";

// Passing URL as a parameter
export const useLambdaApi = (url) => {
  const [data, setData] = useState();
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    const fetchData = async () => {
      const response = await fetch(url);
      const data = await response.json();

      // Set stats
      setIsLoading(false);
      setData(data);
    };
    fetchData();

  // Passing URL as a dependency
  }, [url]);

  // Return 'isLoading' not the 'setIsLoading' function
  return [data, isLoading];
};

// Using Hook into your component
export default function App() {
    // State will be changed if URL changes
    const [data, isLoading] = useLambdaApi('Your URL');

  // Loading indicator
  if (isLoading) return <div>Loading..</div>;

  // Return data when isLoading = false
  return (
    <div className="App">
      // Use data..
    </div>
  );
}

Here is a codesandbox example.

Upvotes: 1

Related Questions