question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal : Promise render hooks

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
otakustaycommented, Jul 22, 2019

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

1reaction
rozaxecommented, Mar 6, 2019

@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 !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found