Proposal : Promise render hooks
See original GitHub issueI was playing with flutter and I found the widget FutureBuilder pretty useful.
This type of component enables to render itself based on the status of a promise.
Typically, this could be used to display a “loading” state while the promise is resolved and then, display the actual component with the freshly resolved value.
Example :
function Hello() {
const snapshot = usePromise(fetchData())
let text = 'Loading'
if (snapshot.hasValue) {
text = snapshot.value
}
return <span>{text}</span>
}
I made an example on CodeSandbox. First, a ‘loading’ text is displayed and after a second (promise resolved), display ‘hello’.
Finally, this is just a wrapper around a useState
but I found it pretty convenient (less boilercode, more expressive).
Do you think this could be a nice addition to the hooks ?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
All About React's Proposed New use() Hook
A feature proposal from the React core team is causing some buzz in the React ecosystem: the new use() hook adds first class...
Read more >The coming use(promise) hook in React | by Irving Juárez
This hooks allows React to add first class support for promises in client-based components, in order to make the code more declarative, which...
Read more >React Hook Async await how to hold rendering on a promise
The end game is trying to make this an action, i want to use this with React redux. But i wanted to start...
Read more >A Proposal for an Alternative Design for Hooks - paulgray.net
The Hook function can be invoked outside the context of a functional component (in a unit test or class component for example). Rendering....
Read more >`usePromise`: A React hook for async data loading with server ...
Once a render completed without any new Promise being scheduled ( throw n), ... The plan is now to take the usePromise helper...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have created a simple library that may match your requirement: https://github.com/ecomfe/react-suspense-boundary
It is not published yet, but internally we use it in some apps in production, it is quite stable and performant
Any thoughts on this will be welcome, we are ready to improve this library to a community friendly and more use case covered one
@ghengeveld Oh thanks for the library ! It does what I said, will use it.
Thanks @brookslybrand for pointing the Suspence feature, I think this issue can be on hold until the fully implementation of it !