admin管理员组文章数量:1404927
I placed a Google Maps API ponent on the index page of my Next js site but it's slowing down the load by a crazy amount (something like 500ms).
Is there a way for me to delay the ponent until after page load using the useEffect hook or in another concise way?
I want to increase the loading speed (and performance score) of the site.
export default function Home() {
return (
<div className={styles.main}>
<Layout>
<main className={styles.content}>
**<div className={styles.mapContainer}>
<Map className={styles.map}/>**
</div>
</main>
</Layout>
</div>
)
}
I placed a Google Maps API ponent on the index page of my Next js site but it's slowing down the load by a crazy amount (something like 500ms).
Is there a way for me to delay the ponent until after page load using the useEffect hook or in another concise way?
I want to increase the loading speed (and performance score) of the site.
export default function Home() {
return (
<div className={styles.main}>
<Layout>
<main className={styles.content}>
**<div className={styles.mapContainer}>
<Map className={styles.map}/>**
</div>
</main>
</Layout>
</div>
)
}
Share
Improve this question
asked May 19, 2020 at 22:56
atman_lensatman_lens
8222 gold badges9 silver badges18 bronze badges
1 Answer
Reset to default 5Next.js provides a dynamic
helper that will only load the ponent when it would render to the page.
https://nextjs/docs/advanced-features/dynamic-import
import dynamic from 'next/dynamic'
const Map = dynamic(() => import('ponents/MapOrWhatever'));
export default function Home() {
return (
<div className={styles.main}>
<Layout>
<main className={styles.content}>
<div className={styles.mapContainer}>
<Map className={styles.map}/>
</div>
</main>
</Layout>
</div>
)
}
You can also specify options for the dynamic
function as the second argument:
const Map = dynamic(() => import('ponents/MapOrWhatever'), {
ssr: false, // do not render this on the server side render
loading: () => <div>Loading Map...</div>, // placeholder ponent
});
If you want to delay rendering (and therefore loading) your ponent until a specific time, then yes, you could use an effect or other hook to toggle rendering the map. As an example:
export default function Home() {
const [showMap, setShowMap] = React.useState(false);
React.useEffect(() => {
// Set the map to load 2 seconds after first render
const timeOut = setTimeout(() => setShowMap(true), 2000);
return () => clearTimeout(timeOut);
}, []);
// <Map> will only load when showMap is true
return (
<div className={styles.main}>
<Layout>
<main className={styles.content}>
{showMap && <div className={styles.mapContainer}>
<Map className={styles.map}/>
</div>}
</main>
</Layout>
</div>
)
}
In practice, it's better to dynamically load ponents that are hidden behind some kind of interaction, like changing tabs in the app to load a new route, or clicking a "View Map" button. Putting it behind a setTimeout
isn't very productive and you're just artificially delaying the loading of ponents arbitrarily without really thinking it through. But, I've included it as an example to show how dynamic ponents only load once they should render.
本文标签: javascriptHow to lazy load map component using useEffect hook for ReactNext js siteStack Overflow
版权声明:本文标题:javascript - How to lazy load map component using useEffect hook for ReactNext js site - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744874496a2629833.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论