React プロジェクトで遅延読み込みとコード分割を実装するためのステップバイステップ ガイド

Patricia Arquette
リリース: 2024-09-30 12:30:03
オリジナル
925 人が閲覧しました

Step by step guide to implementing lazy loading and code splitting in a React project

Here’s a step-by-step guide to implementing lazy loading and code splitting in a React project. We'll create a simple application with two routes, loading components lazily.

Step 1: Create a New React App

If you haven’t already, create a new React app using Create React App:

npx create-react-app lazy-loading-example
cd lazy-loading-example
ログイン後にコピー

Step 2: Install React Router

Install react-router-dom for routing:

npm install react-router-dom
ログイン後にコピー

Step 3: Set Up Lazy Loading and Code Splitting

Create Components

  1. Create a folder named components inside the src directory.
  2. Inside components, create two files: Home.js and About.js.

Home.js

import React from 'react';

const Home = () => {
  return <h2>Home Page</h2>;
};

export default Home;
ログイン後にコピー

About.js

import React from 'react';

const About = () => {
  return <h2>About Page</h2>;
};

export default About;
ログイン後にコピー

Update App.js

Now, modify your App.js file to implement lazy loading and routing:

import React, { Suspense } from 'react';
import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';

// Lazy load components
const Home = React.lazy(() => import('./components/Home'));
const About = React.lazy(() => import('./components/About'));

function App() {
  return (
    <Router>
      <nav>
        <ul>
          <li>
            <Link to="/">Home</Link>
          </li>
          <li>
            <Link to="/about">About</Link>
          </li>
        </ul>
      </nav>

      <Suspense fallback={<div>Loading...</div>}>
        <Switch>
          <Route path="/about" component={About} />
          <Route path="/" component={Home} />
        </Switch>
      </Suspense>
    </Router>
  );
}

export default App;
ログイン後にコピー

Step 4: Run Your Application

Now, run your application to see it in action:

npm start
ログイン後にコピー

Step 5: Test Lazy Loading

  1. Open your browser and navigate to http://localhost:3000.
  2. Click on the "Home" link to see the Home component load.
  3. Click on the "About" link to see the About component load lazily.

Key Points

  • React.lazy is used to dynamically import components, which are loaded only when they are rendered.
  • Suspense is used to handle the loading state, displaying a fallback while the lazy-loaded component is being fetched.
  • This approach significantly reduces the initial load time by splitting the code into smaller chunks.

Additional Enhancements

You can further enhance your setup by:

  • Implementing error boundaries around your lazy-loaded components to catch loading errors.
  • Using advanced routing strategies with React Router for larger applications.

If you need more specific features or additional help, let me know!

以上がReact プロジェクトで遅延読み込みとコード分割を実装するためのステップバイステップ ガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!