Skip to content

Instantly share code, notes, and snippets.

@ChaseH88
Created April 5, 2021 03:17
Show Gist options
  • Select an option

  • Save ChaseH88/df7194c961f5536792edc9e78dbf28f7 to your computer and use it in GitHub Desktop.

Select an option

Save ChaseH88/df7194c961f5536792edc9e78dbf28f7 to your computer and use it in GitHub Desktop.
import React, { FC, lazy, Suspense, useState } from 'react';
// Code Spliting with dynamic imports
const MenuContainer = lazy(() =>
import('./MenuContainer')
.then(({ MenuContainer }) => ({ default: MenuContainer })),
);
const Navigation: FC = (): JSX.Element => {
const [open, toggle] = useState<boolean>(false);
return (
<>
<button
onClick={() => toggle(true)}
>
Open
</button>
{open &&
// Using React Suspense, the component is dynamically imported
<Suspense fallback={<div>Loading...</div>}>
<MenuContainer />
</Suspense>
}
</>
)
}
export { Navigation }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment