Gathering detailed insights and metrics for react-suspense-boundary
Gathering detailed insights and metrics for react-suspense-boundary
npm install react-suspense-boundary
Typescript
Module System
Node Version
NPM Version
70.3
Supply Chain
98.9
Quality
76.8
Maintenance
100
Vulnerability
100
License
TypeScript (99.17%)
JavaScript (0.56%)
Shell (0.26%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
9,262
Last Day
2
Last Week
2
Last Month
88
Last Year
1,637
84 Stars
117 Commits
9 Forks
4 Watching
8 Branches
7 Contributors
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
1
1
39
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.
1npm install react-suspense-boundary
See online demo here: https://ecomfe.github.io/react-suspense-boundary/
You can start demo app yourself by executing:
1npm start
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
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
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}
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 upper Boundary
to be pending until action
is resolved the next time.refresh
is a function to run action
again without removing previously cached result.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 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>
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}
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
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
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
27 existing vulnerabilities detected
Details
Score
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