Home > Back-end >  Uncaught (in promise) TypeError: Cannot read properties of null (reading 'useState')
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'useState')

Time:04-18

I can't figure out why I'm getting this error on const [isLoading, setLoading] = useState(true); It's declared as true from the get go so I can't understand how it's ever undefined

export default async function GetProducts() {
      const [isLoading, setLoading] = useState(true);
      const [products, setProducts] = useState([]);
        useEffect(() => {
          if (isLoading) {
            let url = 'http://127.0.0.1:8000/products'  
            const res = axios.get(url);
            let data = res.json()
      }});
      if (isLoading) {
        return GetLoadingScreen()

  } else {
    return RenderProducts(products)
    }
  }

CodePudding user response:

Consider not marking the React component function as async and marking the function created for useEffect as async instead.

export default function GetProducts() {
  const [isLoading, setLoading] = useState(true);
  const [products, setProducts] = useState([]);
    useEffect(async () => {
      if (isLoading) {
        let url = 'http://127.0.0.1:8000/products'  
        const res = await axios.get(url);
        let data = res.json()
  }});
  if (isLoading) {
    return GetLoadingScreen()
 } else {
    return RenderProducts(products)
    }
  }
  • Related