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.

recommended way to asynchronously fetch data for render call?

See original GitHub issue

Description

~In the constructor on the Bigger Example on the README, it shows to await this.renderComplete, but await can only be used inside of an async function and the constructor function is not marked as async. It seems that the spec doesn’t say anything about a class allowing asynchronous constructor functions.~ This was clarified below. I missed that the async is declared in the click event function inside constructor.

At any rate, the readme doesn’t seem as clear about this, but what is the recommended way to asynchronously fetch data and use it in the _render call?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
justinfagnanicommented, Sep 29, 2021
1reaction
Westbrookcommented, Jun 20, 2018

I discussed this with @mkay581 via slack and got some more information to help clarify an answer. In his context, he’s working with iron-pages to display content and only wants to request the data for a page when it is the active page. To do so he’s using code like so:

<iron-pages selected="0" attr-for-selected="active">
  <page-with-async-data-one active>One</page-with-async-data-one>
  <page-with-async-data-two>Two</page-with-async-data-two>
  <page-with-async-data-three>Three</page-with-async-data-three>
</iron-pages>

In the page-with-async-data-one I’m suggesting he relies less on lit-element and more on general JS to see something like the following manage his data:

static get properties() {
    return {
      active: Boolean
    }
}
connectedCallback() {
   super.connectedCallback();
   this.getAsyncData();
 }
get active() {
  return this._active;
}
set active(active) {
  if (active) this.getAsyncData();
  this._active = active;
}
getAsyncData() {
  if (this._gettingData) return;
  this._gettingData = true;
  //...request async data;
}
dataLoaded(data) {
  this.data = data;
  this._gettingData = false
}
_render() {
  if (this._gettingData) {
    return html `Loading...`;
  }
  return html`Something with all of the data put into a template.`;
}

@pshihn Thanks for the clarification on the rendering system, it made explaining this part much easier. Hope this helps anyone else who runs into this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Handle Async Data Loading, Lazy Loading, and Code ...
Step 1 — Loading Asynchronous Data with useEffect ... In this step, you'll use the useEffect Hook to load asynchronous data into a...
Read more >
Declarative Data Fetching with React Async - Bits and Pieces
Following is a simple example of using <Async> component to fetch data. In the above example fetch API is used to perform the...
Read more >
recommended way to asynchronously fetch data for render call?
The await is inside the event listener function, which is async. this.addEventListener('click', async (e) ...
Read more >
Hierarchy of React Async Components and Fetching Data
First, we will fetch data into our application. In React,, there are two methods to get asynchronous data: The fetch API and the...
Read more >
Fetching Asynchronous Data with React Hooks - Giorgio Polvara
Probably the most common use-case for asynchronous code is to fetch a single resource when the component mounts. We need this all the...
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