Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  113] [ 2]  / answers: 1 / hits: 6605  / 2 Years ago, sun, april 17, 2022, 12:00:00

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)
}
}

More From » reactjs

 Answers
6

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)
}
}

[#189] Wednesday, April 6, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cartersinceren

Total Points: 442
Total Questions: 116
Total Answers: 106

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
cartersinceren questions
Thu, Nov 5, 20, 00:00, 4 Years ago
Thu, Apr 16, 20, 00:00, 4 Years ago
Fri, Feb 1, 19, 00:00, 5 Years ago
Thu, Oct 18, 18, 00:00, 6 Years ago
;