javascript - How to lazy load map component using useEffect hook for ReactNext js site - Stack Overflow

I placed a Google Maps API ponent on the index page of my Next js site but it's slowing down the l

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
Add a ment  | 

1 Answer 1

Reset to default 5

Next.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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744874497a4598465.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信