Created
October 24, 2023 09:02
-
-
Save jpvincent/43b23a421aaa148129a191c2dfa1d8ff to your computer and use it in GitHub Desktop.
lazy load react component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
)} | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tiré du code de Julien Pradet https://www.julienpradet.fr/tutoriels/comment-alleger-son-javascript/