1

I'm migrating a project from React to NextJS which has a layout where the common component is a Leaflet Map.

  1. In Next JS, the "next/dynamic" used to load the map
  2. React used react-router-dom@v6
  3. next layout

Layout code:

import dynamic from "next/dynamic";
import React, { useMemo, useState } from "react";

const DynamicLeafletMap = dynamic(
  () => import("../components/LeafletMap/LeafletMap"),
  {
    ssr: false,
  }
);

function Layout({ children }) {
  return (
    <main>
      <DynamicLeafletMap />
      {children}
    </main>
  );
}

export default Layout;

_app.js code:

import Layout from "../components/Layout";

import "../styles/globals.scss";

function MyApp({ Component, pageProps }) {
  return (
    <Layout>
      <Component {...pageProps} />
    </Layout>
  );
}

export default MyApp;

LeafletMap.jsx

import { MapContainer, TileLayer } from "react-leaflet";
import styles from "./LeafletMap.module.scss";

const initial_position = [38.9637, 35.2433];

function LeafletMap() {
  return (
    <>
        <div className={styles.map}>
          <MapContainer
            center={initial_position}
            zoom={4}
            scrollWheelZoom={true}
            className={styles.map__container}
          >
            <TileLayer
              attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
            />
          </MapContainer>
        </div>
    </>
  );
}

export default LeafletMap;

The problem is that when using "router.push" (for example, clicking on a marker should open a modal window on top of the map), the map is rerendered and reseted. I tried different ways like useMemo, createPortal but this didn't work. Is there a way to fix this behaviour? Maybe there is another framework that provides functionality like "persist layout"?

4
  • I cant see that you have any props going down to the map component. Do you have all the logic in the LeafletMap component?
    – Disco
    Commented Sep 8, 2022 at 15:39
  • Disco, yes. The map is rendered even without a marker tile. I have added the code.
    – FlickDUB
    Commented Sep 8, 2022 at 21:05
  • 1
    That the page rerenders on navigation feels like the correct behaviour. Could you share the code where you use router.push. Did some tests in my project where I just add to query and the map do not rerender. push({ pathname: pathname, query: { ...query, lat: position[0] }, });
    – Disco
    Commented Sep 9, 2022 at 7:53
  • I am not using query parameters. Flow navigation example: 1. Initially on "/" - map is rendered 2. router.push("/{lang}/collection/{slug|id}") opens SSR modal on top of the map, which is rerendered. I found the correct behavior in Remix which uses spa like nesting. But still want to find a way to use NextJS (I saw that the new Layouts RFC hasn't been implemented yet and was looking for a workaround 😕).
    – FlickDUB
    Commented Sep 9, 2022 at 16:11

1 Answer 1

-1

Next13 with new app directory or Remix solves this problem.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Nov 22, 2022 at 22:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.