Installations
npm install react-suspense-boundary
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
20.11.1
NPM Version
10.2.4
Score
70.3
Supply Chain
98.9
Quality
76.8
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (99.17%)
JavaScript (0.56%)
Shell (0.26%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
ecomfe
Download Statistics
Total Downloads
9,262
Last Day
2
Last Week
2
Last Month
88
Last Year
1,637
GitHub Statistics
84 Stars
117 Commits
9 Forks
4 Watching
8 Branches
7 Contributors
Bundle Size
8.21 kB
Minified
2.64 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.0.0
Package Id
react-suspense-boundary@3.0.0
Unpacked Size
48.38 kB
Size
13.50 kB
File Count
30
NPM Version
10.2.4
Node Version
20.11.1
Publised On
22 Mar 2024
Total Downloads
Cumulative downloads
Total Downloads
9,262
Last day
0%
2
Compared to previous day
Last week
-92.6%
2
Compared to previous week
Last month
-39.3%
88
Compared to previous month
Last year
0.1%
1,637
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
1
Dev Dependencies
39
react-suspense-boundary
A boundary component working with suspense and error
Version 2.x is implemented on use-sync-external-store to align with future official suspense data fetching.
Install
1npm install react-suspense-boundary
Demo
See online demo here: https://ecomfe.github.io/react-suspense-boundary/
You can start demo app yourself by executing:
1npm start
Usage
Basic
1import {Boundary, CacheProvider, useResource} from 'react-suspense-boundary'; 2 3// Create or import an async function 4const fetchInfo = ({id}) => fetch(`/info/${id}`).then(response => response.json()); 5 6// Implement your presentational component 7function Info({id}) { 8 // Call `useResource` to fetch, note the return value is an array 9 const [info] = useResource(fetchInfo, {id}); 10 11 // There is no `loading` branch, push returned object immediately to render 12 return ( 13 <div> 14 {info.id}: {info.name} 15 </div> 16 ); 17}; 18 19// Data is stored inside `CacheProvider`, suspending state is controlled with `Boundary` 20export default function App() => ( 21 <CacheProvider> 22 <Boundary> 23 <Info /> 24 </Boundary> 25 </CacheProvider> 26);
CacheProvider
CacheProvider
is by its name a cache context where we store all resources loaded by its children.
The simpliest way to use CacheProvider
is to provider an application level top cache:
1import {render} from 'react-dom'; 2import {CacheProvider} from 'react-suspense-boundary'; 3import {App} from './components/App'; 4 5render( 6 <CacheProvider> 7 <App /> 8 </CacheProvider>, 9 document.getElementById('root') 10);
For some more complex applications, you may want to restrict data caching in a smaller scope, e.g. route level, and expire cached responses on unmount, you can put CacheProvider
anywhere you want to make a shared cache.
Boundary
Boundary
components defines a boundary in your view, within a boundary all async resource fetchings and errors are collected to form a loading or error indicator.
Usually we would have mulitple Boundary
inside a CacheProvider
, that is, users see different sections loading individually, but all resources are shared.
A Boundary
component receives props below:
1interface RenderErrorOptions { 2 recover: () => void; 3} 4 5interface BoundaryProps { 6 // When any of async progress is pending, boundary will render this element 7 pendingFallback: ReactNode; 8 // When any error are received, will render this function 9 renderError(error: Error, options: RenderErrorOptions): ReactNode; 10 // When any error are catched, will call this function 11 onErrorCaught(error: Error, info: ErrorInfo): void; 12}
useResource
The useResource
hook is used to inspect an async function within a boundary:
1type Resource<T> = [ 2 T, 3 { 4 expire(): void; 5 refresh(): void; 6 } 7]; 8 9function useResource<I, O>(action: (input: I) => Promise<O>, params: I): Resource<O>; 10function useConstantResource<O>(action: () => Promise<O>): Resource<O>;
Unlike other async hooks, useResource
returns the result "immediately", there is no pending
or loading
state, no exception will throw.
Other than the result itself, the second object of useResource
's returned array is a a bunch of functions to manually control the cache:
expire
will immediately remove the cached result, causing the upperBoundary
to be pending untilaction
is resolved the next time.refresh
is a function to runaction
again without removing previously cached result.
Default configuration
BoundaryConfigProvider
provides default configurations to pendingFallback
, renderError
and onErrorCaught
props.
1import {Spin} from 'antd'; 2import {BoundaryConfigProvider} from 'react-suspense-boundary'; 3 4const defaultPendingFallback = <Spin />; 5 6const defaultRenderError = error => ( 7 <div> 8 {error.message} 9 </div> 10); 11 12const App = () => { 13 <BoundaryConfigProvider 14 pendingFallback={defaultPendingFallback} 15 renderError={defaultRenderError} 16 > 17 {/* All Boundary elements inside it receives default configurations */} 18 </BoundaryConfigProvider> 19}
Preload
Preload is much like resource fetching, they can be "immediately" fired within a render function:
1function usePreloadResource<I, O>(action: (input: I) => Promise<O>, params: I): void; 2function usePreloadConstantResource<O>(action: () => Promise<O>): void;
Preload fires resource fetching process but not abort current render.
You can also get a preload
function using usePreloadCallback
hook to preload any resources in effect or event handlers:
1const preload = usePreloadCallback(); 2 3<Button onMouseEnter={() => preload(fetchList, {pageIndex: currentPageIndex + 1})}> 4 Next Page 5</Button>
Create Your Own Cache
react-suspense-boundary
's built-in CacheProvider
references a single context type, that is, you are unable to access multiple caches in a single component:
1<CacheProvider> 2 <div> 3 <CacheProvider> 4 <MyResource /> 5 </CacheProvider> 6 </div> 7</CacheProvider>
By default, there is no way to a make MyResource
to load one resource into the outer CacheProvider
and another into the inner CacheProvider
.
To solve this issue, we provide a create()
function to create a custom set of providers and hooks, with a different context type so that you can use them simultaneously:
1import {CacheProvider, create, useConstantResource} from 'react-suspense-boundary'; 2 3const { 4 CacheProvider: GlobalCacheProvider, 5 useConstantResource: useGlobalConstantResource, 6} = create(); 7 8function MyResource() { 9 // Put current user resource into global cache 10 const [currentUser] = useGlobalConstantResource(fetchCurrentUser); 11 // And other resources into local one 12 const [dataSource] = useConstantResource(fetchList); 13 14 return ( 15 // ... 16 ); 17} 18 19<GlobalCacheProvider> 20 <CacheProvider> 21 <MyResource /> 22 </CacheProvider> 23</GlobalCacheProvider>
create
function also accepts an option object to customize context's display name:
1interface CreateOptions { 2 cacheContextDisplayName?: string; 3 configContextDisplayName?: string; 4}
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 2/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Reason
27 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-8hc4-vh64-cxmj
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-ghr5-ch3p-vcr6
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-3f95-r44v-8mrg
- Warn: Project is vulnerable to: GHSA-28xr-mwxg-3qc8
- Warn: Project is vulnerable to: GHSA-9p95-fxvg-qgq2
- Warn: Project is vulnerable to: GHSA-9w5j-4mwv-2wj8
- Warn: Project is vulnerable to: GHSA-8jhw-289h-jh2g
- Warn: Project is vulnerable to: GHSA-64vr-g452-qvp3
- Warn: Project is vulnerable to: GHSA-9cwx-2883-4wfx
- Warn: Project is vulnerable to: GHSA-vg6x-rcgg-rjx6
- Warn: Project is vulnerable to: GHSA-9crc-q9x8-hgqq
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
1.7
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More