React Hooks: useEffect() is called twice Next.js and React
In this blog post, we will explore how to fix problem with useeffect() hook called twice during the Page load in next.js.
Code
"use client"
import { useEffect, useState } from "react";
export default function Home() {
const [products, setProducts]=useState([]);
useEffect(() => {
fetch("https://dummyjson.com/products")
.then(response => response.json())
.then(data => setProducts(data.products))
},[])
return (
);
}
Refresh the page and check network tab. Fetch/XHR
Solution
- Go to next.config.mjs and add reactStrictMode: false
Refresh the page and check network tab. Fetch/XHR
Conclusion
In this blog post, we learned how to fix the useEffect() hook calling twice during page load in Next.js application.
Comment on this Post if you have any questions.
Made with VideoToBlog




Comments
Post a Comment