Replies: 1 comment 3 replies
-
Update (2023-10-28): In VanJS, UI components need to be declared synchronously. Async components make things much more complicated, especially when GC comes to play. If your component depends on data that is fetched asynchronously, here is the pattern that you can use: const data = van.state({})
const fetchData = async () => {
data.val = {loading: true}
const result = await fetch(...)
data.val = {result}
}
const Component = () => div(
() => data.val.loading ?
<Render UI elements to indicate we're loading the data> :
<Render UI elements based on `data.val.result`>
) This sample app is built based on this pattern. Hope it helps :-) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, VanJS component cannot be a
async
function now, but it is really common to fetch data when render a component.I have two ideas about that.
Suspense
, which will display a fallback until its children have finished loadingI know we can do that in this way:
But the DX will be really good if we support async component or Suspense.
Async component support version will be like:
Suspense support version will be like:
Could you please give any suggestions.
PS: I'm happy to commit this and I'm trying to implement
Suspense
component now, but I got some problem.Beta Was this translation helpful? Give feedback.
All reactions