Issue with rendering items fetched via a promise #3269
Replies: 2 comments 4 replies
-
I would suggest storing the data on the element object, such that reconnecting to the DOM will not trigger a refetch. I'm also noticing that the emoji data depends on the category and version, so we can cache by those as well, and do that whenever the dependencies change. @property() category?: Category;
@property() emojiVersion?: Version;
@state() private data?: EmojiData;
#map = new Map<`${Category}-${Version}`, EmojiData>();
updated(changed) {
if (changed.has('category') || changed.has('emojiVersion'))
this.#updateIcons();
}
async #updateIcons() {
const key = `${this.category}-${this.version}`;
if (!this.#map.has(key))
this.#map.set(key, await somehowFetchCategory(this.category, this.emojiVersion);
this.data = this.#map.get(key);
} |
Beta Was this translation helpful? Give feedback.
-
Can you make a small reproduction at all? I'm having a hard time what's happening with the preview data flow. The database can be replaced by a local async function. One thing that could be happening is if you're reassigning the |
Beta Was this translation helpful? Give feedback.
-
I think I may be misunderstanding something about how Lit renders, but I'm having an issue.
I have an emoji picker element. It renders a grid of emojis. These emojis area loaded from a database asynchronously (so a
Promise
is involved).I am firing off the request in the
connectedCallback
. It makes the async call and waits on the promise, then it updates a property of the classemojis
which is decorated with thestate()
decorator:My render method calls to a helper method which renders either the emojis, if they're loaded, or nothing:
This all works as expected.
Here's my problem. I am adding a preview element. The main picker has a state property that keeps track of the currently hovered emoji. When the mouseover event is triggered on an emoji, it fires a custom event which is received by the root emoji picker element. It updates the state property to update the hovered emoji.
When this happens, all my emojis seem to be destroyed and re-rendered again. The database call is made again to load the emojis. I had assumed that using
@state()
on the emoji list would remember the previous value, but every time through it comes back as undefined and has to load again.I assume this means that during the re-rendering process of the root emoji picker element, this element is getting completely destroyed? I'm not sure what I am missing here but since that emoji data never changes, I am looking for how I can "remember" the loaded emojis and not discard and re-load them when a parent is rendered.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions