Skip to content

No atoms found when lazy loading #212

@min-ben

Description

@min-ben

I have this pattern I use for DevTools extensions - I load based on environment variables (which i validate with zod). I've noticed that when I do this, the extension loads file, but I get no atoms. As soon as I move it out of the lazy, I can see it does work and find all atoms

import React, { Suspense } from "react";
import { env } from "#env.ts";
import { jotaiStore } from "#store/atoms/jotaiStore.ts";

const LazyDevTools =
  import.meta.env.PROD || !env.enableJotaiDevtools
    ? () => null
    : React.lazy(() =>
        Promise.all([
          import("jotai-devtools"),
          import("jotai-devtools/styles.css?inline"),
        ]).then(([{ DevTools }, jotaiCss]) => ({
          default: () => {
            return (
              <>
                <style>{jotaiCss.default}</style>
                <DevTools
                  store={jotaiStore}
                  position="bottom-right"
                  theme="dark"
                  options={{
                    // It defaults to infinity which will cause the extension to crash when storing a lot of data
                    snapshotHistoryLimit: 30,
                  }}
                />
              </>
            );
          },
        })),
      );

export const JotaiDevTools = () => (
  <Suspense fallback={null}>
    <LazyDevTools store={jotaiStore} />
  </Suspense>
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions