Convert Figma logo to code with AI

gregberge logoloadable-components

The recommended Code Splitting library for React ✂️✨

7,641
381
7,641
39

Top Related Projects

:hourglass_flowing_sand: A higher order component for loading components with promises.

The recommended Code Splitting library for React ✂️✨

🚀 The final answer to a React Universal Component: simultaneous SSR + Code Splitting

Declarative routing for React

Quick Overview

Loadable Components is a React code-splitting library that makes it easy to split your application into smaller chunks and load them on demand. It provides a simple API for dynamic imports in React applications, improving performance by reducing initial bundle size and enabling faster loading times.

Pros

  • Easy to use with a simple API that integrates well with React
  • Supports Server-Side Rendering (SSR) out of the box
  • Provides a webpack plugin for automatic chunk optimization
  • Offers a babel plugin for automatic code-splitting

Cons

  • Limited documentation for advanced use cases
  • May require additional configuration for complex setups
  • Potential learning curve for developers new to code-splitting concepts
  • Some features might conflict with other React optimization libraries

Code Examples

  1. Basic usage with React components:
import loadable from '@loadable/component'

const OtherComponent = loadable(() => import('./OtherComponent'))

function MyComponent() {
  return (
    <div>
      <OtherComponent />
    </div>
  )
}
  1. Using with React Suspense:
import loadable from '@loadable/component'

const OtherComponent = loadable(() => import('./OtherComponent'), {
  fallback: <div>Loading...</div>
})

function MyComponent() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <OtherComponent />
    </Suspense>
  )
}
  1. Dynamic import with custom loading component:
import loadable from '@loadable/component'
import Loading from './Loading'

const OtherComponent = loadable(() => import('./OtherComponent'), {
  fallback: <Loading />
})

function MyComponent() {
  return (
    <div>
      <OtherComponent />
    </div>
  )
}

Getting Started

  1. Install the package:
npm install @loadable/component
  1. Use in your React application:
import loadable from '@loadable/component'

const OtherComponent = loadable(() => import('./OtherComponent'))

function App() {
  return (
    <div>
      <h1>My App</h1>
      <OtherComponent />
    </div>
  )
}
  1. For SSR support, add the Babel and webpack plugins:
npm install @loadable/babel-plugin @loadable/webpack-plugin

Then configure your Babel and webpack settings accordingly.

Competitor Comparisons

:hourglass_flowing_sand: A higher order component for loading components with promises.

Pros of react-loadable

  • Simpler API with fewer configuration options, making it easier to get started
  • Built-in support for server-side rendering without additional setup
  • Smaller bundle size, which can be beneficial for smaller projects

Cons of react-loadable

  • No longer actively maintained, with the last update in 2018
  • Lacks support for newer React features and patterns
  • Limited customization options compared to loadable-components

Code Comparison

react-loadable:

import Loadable from 'react-loadable';

const LoadableComponent = Loadable({
  loader: () => import('./MyComponent'),
  loading: LoadingComponent,
});

loadable-components:

import loadable from '@loadable/component';

const LoadableComponent = loadable(() => import('./MyComponent'), {
  fallback: <LoadingComponent />,
});

Both libraries provide similar basic functionality for code splitting in React applications. However, loadable-components offers more advanced features and better compatibility with modern React practices.

loadable-components supports newer React features, provides more customization options, and is actively maintained. It also offers better integration with build tools and has a more extensive ecosystem of plugins and utilities.

While react-loadable may still be suitable for simpler projects or those with legacy codebases, loadable-components is generally recommended for new projects and those requiring more advanced code-splitting capabilities.

The recommended Code Splitting library for React ✂️✨

Pros of loadable-components

  • Simplified code splitting and dynamic imports in React applications
  • Supports server-side rendering (SSR) out of the box
  • Provides a flexible API for customizing loading behavior

Cons of loadable-components

  • May have a slightly steeper learning curve for beginners
  • Requires additional configuration for optimal performance in some cases

Code Comparison

loadable-components:

import loadable from '@loadable/component'

const OtherComponent = loadable(() => import('./OtherComponent'))

function MyComponent() {
  return (
    <div>
      <OtherComponent />
    </div>
  )
}

Key Differences

Since both repositories refer to the same project (loadable-components), there are no actual differences to compare. The repository gregberge/loadable-components is the main and only repository for the loadable-components project.

Summary

loadable-components is a popular library for implementing code splitting and lazy loading in React applications. It offers a straightforward API for dynamically importing components, supports server-side rendering, and provides flexibility for customizing loading behavior. While it may require some initial setup and learning, it can significantly improve the performance and loading times of React applications by enabling efficient code splitting.

🚀 The final answer to a React Universal Component: simultaneous SSR + Code Splitting

Pros of react-universal-component

  • More flexible configuration options for advanced use cases
  • Built-in support for Redux integration
  • Includes additional features like error handling and loading indicators

Cons of react-universal-component

  • Less active maintenance and updates compared to loadable-components
  • Steeper learning curve due to more complex API
  • May have performance overhead for simpler use cases

Code Comparison

react-universal-component:

import universal from 'react-universal-component'

const UniversalComponent = universal(() => import('./MyComponent'), {
  loading: LoadingComponent,
  error: ErrorComponent,
  minDelay: 300
})

loadable-components:

import loadable from '@loadable/component'

const LoadableComponent = loadable(() => import('./MyComponent'), {
  fallback: <LoadingComponent />
})

Both libraries provide similar core functionality for code splitting and lazy loading in React applications. react-universal-component offers more advanced features and configuration options, which can be beneficial for complex projects. However, loadable-components has a simpler API, more active maintenance, and better integration with server-side rendering frameworks like Next.js. The choice between the two depends on specific project requirements and the desired balance between simplicity and advanced features.

Declarative routing for React

Pros of React Router

  • Comprehensive routing solution with built-in components for navigation
  • Supports nested routing and dynamic route matching
  • Large community and extensive documentation

Cons of React Router

  • Heavier bundle size compared to Loadable Components
  • Primarily focused on routing, not code splitting
  • Steeper learning curve for complex routing scenarios

Code Comparison

React Router:

import { BrowserRouter, Route, Link } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Route path="/about" component={About} />
      <Link to="/about">About</Link>
    </BrowserRouter>
  );
}

Loadable Components:

import loadable from '@loadable/component';

const About = loadable(() => import('./About'));

function App() {
  return (
    <div>
      <About />
    </div>
  );
}

Key Differences

  • React Router focuses on routing and navigation
  • Loadable Components specializes in code splitting and lazy loading
  • React Router provides a more complete routing solution
  • Loadable Components offers a simpler API for component-level code splitting

Use Cases

  • Choose React Router for complex routing needs in single-page applications
  • Opt for Loadable Components when prioritizing performance through code splitting

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

loadable-components

React code splitting made easy. Reduce your bundle size without stress ✂️✨.

License npm package npm downloads Build Status Code style Dependencies DevDependencies Small size

npm install @loadable/component

Docs

See the documentation at loadable-components.com for more information about using Loadable Components!

Quicklinks to some of the most-visited pages:

Example

import loadable from '@loadable/component'

const OtherComponent = loadable(() => import('./OtherComponent'))

function MyComponent() {
  return (
    <div>
      <OtherComponent />
    </div>
  )
}

Supporting Loadable Components

Loadable Components is an MIT-licensed open source project. It's an independent project with ongoing development made possible thanks to the support of these awesome backers. If you'd like to join them, please consider:

License

Licensed under the MIT License, Copyright © 2017-present Greg Bergé.

See LICENSE for more information.

NPM DownloadsLast 30 Days