Skip to content

Instantly share code, notes, and snippets.

@jpvincent
Created October 24, 2023 09:02
Show Gist options
  • Save jpvincent/43b23a421aaa148129a191c2dfa1d8ff to your computer and use it in GitHub Desktop.
Save jpvincent/43b23a421aaa148129a191c2dfa1d8ff to your computer and use it in GitHub Desktop.
lazy load react component
import { lazy, useState, useTransition } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
const Modal = lazy(() => import('./Modal.jsx'));
export default function App() {
const [isPending, startTransition] = useTransition();
const [opened, setOpened] = useState(false);
function openModal() {
startTransition(() => {
setOpened(true);
});
}
function onLoadError() {
// gestion en cas d'erreur de connexion ici
}
return (
<>
<button onClick={openModal}>{isPending ? 'Loading' : 'Open'}</button>
{opened && (
<ErrorBoundary onError={onLoadError}>
<Modal />
</ErrorBoundary>
)}
</>
);
}
@jpvincent
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment