React网站加载缓慢且按钮跳转失效问题求助
Hey there! Let's tackle your two issues one by one to get your React site running smoothly.
1. 按钮跳转失效问题
Your current approach using onClick to return a <NavLink> component won't work—because returning a component in an onClick handler doesn't render it or trigger the navigation action. Here are two straightforward fixes:
方法一:用NavLink包裹Button组件
Instead of nesting NavLink inside the onClick, wrap the Button with NavLink directly. This way, clicking the Button will trigger the NavLink's navigation behavior:
<NavLink to="/products"> <Button variant="outline-dark">See our Products</Button> </NavLink>
方法二:使用useNavigate钩子
If you prefer to keep the Button's onClick logic, use the useNavigate hook from react-router-dom to programmatically navigate:
import React from "react" import "./HomePage.css"; // ... other imports import { NavLink, useNavigate } from 'react-router-dom'; // 新增useNavigate导入 import { Button } from 'react-bootstrap'; const Home = () => { const navigate = useNavigate(); // 初始化navigate函数 return ( <> {/* ... 其他页面内容 ... */} <Button variant="outline-dark" onClick={() => navigate("/products")} // 调用navigate跳转 > See our Products </Button> {/* ... 其他按钮同理修改 ... */} </> ) } export default Home;
Either of these methods will fix the navigation issue across all your pages—just apply the same change to your "Know More" and "Contact us!" buttons.
2. 网站加载速度慢问题
You've already resized images, which is a great start! Let's add more optimizations to boost load times:
- Optimize image formats: Convert your JPG/PNG images to modern formats like WebP or AVIF—they offer better compression without losing quality. Tools like Squoosh can help you do this easily.
- Implement image lazy loading: Add the native
loading="lazy"attribute to your<img>tags to defer loading offscreen images until they're needed:<img src={homeImage1} alt="breakfast bowl" loading="lazy" /> - Code splitting with React.lazy: Split your app into smaller chunks so only the code needed for the current page loads initially. For example, if your Products page is a separate component:
const Products = React.lazy(() => import('./Products')); // Then use it with Suspense in your router: <Suspense fallback={<div>Loading...</div>}> <Route path="/products" element={<Products />} /> </Suspense> - Check for unnecessary dependencies: Audit your
package.jsonto ensure you're not including unused libraries that bloat your bundle size. - Enable compression on GitHub Pages: GitHub Pages automatically gzips assets, but double-check that your build process is outputting compressed files. You can also use tools like
gzip-cliduring your build step to compress static assets. - Use a CDN for assets: While GitHub Pages uses a CDN, make sure your images are hosted efficiently—you could also consider using a service like Cloudinary to serve optimized, cached images.
These steps should significantly improve your site's load speed and navigation experience.
内容的提问来源于stack exchange,提问作者Joana Oliveira

