Hooks that allow synchronous execution of Async functions via React.Suspension
$ npm install use-suspense-hooks
$ yarn add use-suspense-hooks
// This component is loaded dynamically
const OtherComponent: React.FC = () => {
const result = useSuspenseValue(`unique-key`, async () => {
// Asynchronous processing such as data fetching
return await asyncFunction()
})
return (
<div>{result}</div>
)
}
function MyComponent() {
return (
// Displays <Spinner> until OtherComponent loads
<React.Suspense fallback={<Spinner />}>
<div>
<OtherComponent />
</div>
</React.Suspense>
);
}
- Fork it ( https://github.com/sskmy1024y/use-suspense-hooks/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
The MIT License (MIT)
- Copyright (c) 2022 sskmy1024y