docs: write page for dynamic function (#503)

This commit is contained in:
Jacob Marshall
2025-02-05 14:01:49 +00:00
committed by GitHub
parent 1a3c9a7fbe
commit 7ea8414d6c
4 changed files with 61 additions and 28 deletions
@@ -169,18 +169,6 @@ export const sidebarElements: Array<SidebarElement> = [
},
],
},
{
type: 'element',
label: 'Core concepts',
href: '#focus',
children: [
{
type: 'element',
label: 'Multithreading',
href: '/documentation/core-concepts/multithreading',
},
],
},
{ type: 'divider' },
{ type: 'title', label: 'API reference' },
{
@@ -197,13 +185,18 @@ export const sidebarElements: Array<SidebarElement> = [
},
{
type: 'element',
label: 'Hooks',
label: 'Functions',
href: '#focus',
children: [
{
type: 'element',
label: 'dynamic',
href: '/documentation/functions/dynamic',
},
{
type: 'element',
label: 'useRouter',
href: '/documentation/hooks/use-router',
href: '/documentation/functions/use-router',
},
],
},
@@ -1,14 +0,0 @@
import MetaTags from '@/components/MetaTags'
<MetaTags
title="Tuono - Multithreading"
canonical="https://tuono.dev/documentation/core-concepts/multithreading"
/>
import Breadcrumbs from '@/components/Breadcrumbs'
<Breadcrumbs breadcrumbs={[{ label: 'Core concepts' }]} />
# Multithreading
TODO
@@ -0,0 +1,54 @@
import MetaTags from '@/components/MetaTags'
<MetaTags
title="Tuono - dynamic"
canonical="https://tuono.dev/documentation/functions/dynamic"
description="Lazy load components using the 'dynamic' function"
/>
import Breadcrumbs from '@/components/Breadcrumbs'
<Breadcrumbs breadcrumbs={[{ label: 'dynamic' }]} />
# dynamic
Tuono supports lazy loading through the `dynamic` function, which uses `Suspense` and `React.lazy` to load components lazily with fallback support.
Lazy loading helps reduce the amount of JavaScript required for the initial page render by deferring the loading of components and libraries until they are needed.
It's most effective when deferring content that isn't initially visible, like a component below the fold or one in a modal.
## Usage
Here is an example using all the exposed options as referenced below.
```typescript jsx
import { dynamic } from 'tuono'
const LazyComponent = dynamic(() => import('./MyComponent'), {
ssr: false,
loading: () => <div>Loading...</div>,
})
function App() {
return (
<div>
<h1>Welcome</h1>
<LazyComponent />
</div>
)
}
```
> ⚠️ Note that the first argument must be a function that returns a promise (e.g., `() => import('./MyComponent')`), not a direct call to `import('./MyComponent')`, which would execute immediately instead of lazily loading.
> 💡 Its good practice to ensure that your fallback component matches the dimensions of the actual component. This helps prevent layout shifts and the ugly resizing of modals as their content loads.
## Options
| Name | Type | Default value | Description |
| -------------- | ------------------------------------------------------------------------ | ------------- | ------------------------------------------------------------------------------ |
| `importFn` | `() => Promise<React.ComponentType<T> \| ComponentModule<T>>` (Required) | - | Function to import your component. |
| `opts` | `DynamicOptions` | - | Optional config object for additional control. |
| `opts.ssr` | `boolean` | `true` | Whether to enable server-side rendering for the component. |
| `opts.loading` | `React.ComponentType<unknown>` | `null` | A fallback component to show while the lazy-loaded component is being fetched. |