[WIP] [Feature]: useLazy(Comp)
- [x] Understand the existing lazy implementation and testing structure
- [x] Design the useLazy hook interface based on issue requirements
- [x] Implement LazyProvider context to track lazy component states globally
- [x] Implement useLazy hook that can track loading states of lazy components
- [x] Add the hook to support preloading and state tracking outside of Suspense
- [x] Create tests for the new useLazy functionality
- [x] Update exports to include new useLazy hook and LazyProvider
- [ ] Test the implementation to ensure it works as expected
- [ ] Update documentation if needed
Implementation Details
New Components Added:
- LazyProvider: Context provider that manages loading states of lazy components globally
-
useLazy: Hook that accepts a lazy component and returns
{ isLoading, isLoaded, load }
Key Features:
- ✅ Track loading state outside of Suspense boundaries
- ✅ Preload lazy components via
load()method - ✅ Prevent duplicate loading when already loading/loaded
- ✅ Independent state tracking for multiple lazy components
- ✅ Error handling that resets loading state on failures
Usage Example:
import { lazy, useLazy, LazyProvider } from '@suspensive/react'
const Page1 = lazy(() => import('./Page1'))
const NavItemToPage1 = () => {
const lazyPage1 = useLazy(Page1)
return (
<Link onMouseEnter={() => lazyPage1.load()} to='/page1'>
to Page1 {lazyPage1.isLoading ? 'loading...' : lazyPage1.isLoaded ? 'loaded' : 'not loaded'}
</Link>
)
}
Fixes #1666.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
The latest updates on your projects. Learn more about Vercel for GitHub.
| Project | Deployment | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| v2.suspensive.org | Preview | Comment | Sep 24, 2025 4:01pm | |
| v3.suspensive.org | Sep 24, 2025 4:01pm | |||
| visualization.suspensive.org | Preview | Comment | Sep 24, 2025 4:01pm |
⚠️ No Changeset found
Latest commit: 40ecb1ffabf399745da99593e0daefbb420f3e57
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
People can be co-author:
| Candidate | Reasons | Count | Add this as commit message |
|---|---|---|---|
| @Copilot | https://github.com/toss/suspensive/pull/1749 | 1 | Co-authored-by: Copilot <[email protected]> |