# 动态引入
Examples
Dynamic Import
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:
import { useState } from 'react'
const names = ['Tim', 'Joe', 'Bel', 'Max', 'Lee']
export default function Page() {
const [results, setResults] = useState()
return (
<div>
<input
type="text"
placeholder="Search"
onChange={async (e) => {
const { value } = e.currentTarget
// Dynamically load fuse.js
const Fuse = (await import('fuse.js')).default
const fuse = new Fuse(names)
setResults(fuse.search(value))
}}
/>
<pre>Results: {JSON.stringify(results, null, 2)}</pre>
</div>
)
}
You can think of dynamic imports as another way to split your code into manageable chunks.
+ 
关注公众号,获取验证码 !
验证码: