admin管理员组文章数量:1392054
I'm trying to build a website in NextJS. In order to improve the UX, I created a Loader component.
The problem
The problem is that, when I start the Next server, even though the page is correctly compiled and ready, i can only see the loader gif. But when I refresh the page everything fix itself and the website loader disappear. How can I fix this first load?
What I tried
This is the code I've created for the components/LoaderWrapper.tsx
:
"use client";
import { useState, useEffect } from "react";
import { usePathname } from "next/navigation";
import { motion, AnimatePresence } from "framer-motion";
export default function LoadingWrapper({ children }: { children: React.ReactNode }) {
const [isLoading, setIsLoading] = useState(true);
const pathname = usePathname();
useEffect(() => {
setIsLoading(true);
const timer = setTimeout(() => setIsLoading(false), 500); // Breve delay per evitare flickering
return () => clearTimeout(timer);
}, [pathname]);
return (
<AnimatePresence mode="wait">
{isLoading ? (
<motion.div
key="loading"
className="fixed inset-0 flex justify-center items-center bg-white"
initial={{ opacity: 1 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.5, ease: "easeOut" }}
>
<img src="/loader/loading.gif" alt="Loading..." className="w-20 h-20" />
<p>La tua pagina si sta caricando. Attendi...</p>
</motion.div>
) : (
<motion.div
key="content"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.5, ease: "easeOut" }}
className="w-full"
>
{children} {/* Ora tutto si carica insieme */}
</motion.div>
)}
</AnimatePresence>
);
}
本文标签: typescriptLoader does not disappear when home is loadedStack Overflow
版权声明:本文标题:typescript - Loader does not disappear when home is loaded - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744782967a2624824.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论