Dynamic Import

Examples

Next.js supports ES2020 dynamic import() for JavaScript. With it you can import JavaScript modules dynamically and work with them. They also work with SSR.

In the following example, we implement fuzzy search using fuse.js and only load the module dynamically in the browser after the user types in the search input:

  1. import { useState } from 'react'
  2. const names = ['Tim', 'Joe', 'Bel', 'Max', 'Lee']
  3. export default function Page() {
  4. const [results, setResults] = useState()
  5. return (
  6. <div>
  7. <input
  8. type="text"
  9. placeholder="Search"
  10. onChange={async (e) => {
  11. const { value } = e.currentTarget
  12. // Dynamically load fuse.js
  13. const Fuse = (await import('fuse.js')).default
  14. const fuse = new Fuse(names)
  15. setResults(fuse.search(value))
  16. }}
  17. />
  18. <pre>Results: {JSON.stringify(results, null, 2)}</pre>
  19. </div>
  20. )
  21. }

You can think of dynamic imports as another way to split your code into manageable chunks.

React components can also be imported using dynamic imports, but in this case we use it in conjunction with next/dynamic to make sure it works just like any other React Component. Check out the sections below for more details on how it works.

Basic usage

In the following example, the module ../components/hello will be dynamically loaded by the page:

  1. import dynamic from 'next/dynamic'
  2. const DynamicComponent = dynamic(() => import('../components/hello'))
  3. function Home() {
  4. return (
  5. <div>
  6. <Header />
  7. <DynamicComponent />
  8. <p>HOME PAGE is here!</p>
  9. </div>
  10. )
  11. }
  12. export default Home

DynamicComponent will be the default component returned by ../components/hello. It works like a regular React Component, and you can pass props to it as you normally would.

With named exports

If the dynamic component is not the default export, you can use a named export too. Consider the module ../components/hello.js which has a named export Hello:

  1. export function Hello() {
  2. return <p>Hello!</p>
  3. }

To dynamically import the Hello component, you can return it from the Promise returned by import(), like so:

  1. import dynamic from 'next/dynamic'
  2. const DynamicComponent = dynamic(() =>
  3. import('../components/hello').then((mod) => mod.Hello)
  4. )
  5. function Home() {
  6. return (
  7. <div>
  8. <Header />
  9. <DynamicComponent />
  10. <p>HOME PAGE is here!</p>
  11. </div>
  12. )
  13. }
  14. export default Home

With custom loading component

An optional loading component can be added to render a loading state while the dynamic component is being loaded. For example:

  1. import dynamic from 'next/dynamic'
  2. const DynamicComponentWithCustomLoading = dynamic(
  3. () => import('../components/hello'),
  4. { loading: () => <p>...</p> }
  5. )
  6. function Home() {
  7. return (
  8. <div>
  9. <Header />
  10. <DynamicComponentWithCustomLoading />
  11. <p>HOME PAGE is here!</p>
  12. </div>
  13. )
  14. }
  15. export default Home

With no SSR

You may not always want to include a module on server-side. For example, when the module includes a library that only works in the browser.

Take a look at the following example:

  1. import dynamic from 'next/dynamic'
  2. const DynamicComponentWithNoSSR = dynamic(
  3. () => import('../components/hello3'),
  4. { ssr: false }
  5. )
  6. function Home() {
  7. return (
  8. <div>
  9. <Header />
  10. <DynamicComponentWithNoSSR />
  11. <p>HOME PAGE is here!</p>
  12. </div>
  13. )
  14. }
  15. export default Home