Re:VanJS: A 0.9KB JavaScript UI framework
by seanlaff on Thursday December 21, 2023 @02:03PM
(#38725137)
Hmm, maybe! Is that the idiomatic way to do async? I was thinking something along the lines of this, which react devs will have written a variation of plenty of times :)
[ Parent
| on HN ]
Re:VanJS: A 0.9KB JavaScript UI framework
by taoxin on Thursday December 21, 2023 @02:29PM
(#38725521)
You can do with VanJS in this way as well. `set...` functions in React map to `....val = ...` in VanJS (which I think is more intuitive). But the `Await` component is a good abstraction so that you can specify what to show when the resources are being fetched and what to show when there are errors while fetching the resources.
Re:VanJS: A 0.9KB JavaScript UI framework
by seanlaff on Thursday December 21, 2023 @04:22PM
(#38727144)
Would we expect the list to re-render once the fetch finishes? This may just be years of react poisoning my brain
const Stargazers = () => {
const stargazers = van.state([]);
fetch(`https://api.github.com/repos/vanjs-org/van/stargazers?per_page=5`)
.then((r) => r.json())
.then((r) => stargazers.val = r);
return ul(
stargazers.val.map((s) => li(s.login))
);
};
Re:VanJS: A 0.9KB JavaScript UI framework
by taoxin on Thursday December 21, 2023 @04:54PM
(#38727567)
Yes. This is how VanJS works. Code with VanJS is often more concise compared to equivalent code with React.
Re:VanJS: A 0.9KB JavaScript UI framework
by seanlaff on Thursday December 21, 2023 @06:33PM
(#38728690)
Ah ok! Very cool. Maybe I'm still missing a tiny piece of syntax? I don't see any output when I run that code in the fiddle
Re:VanJS: A 0.9KB JavaScript UI framework
by taoxin on Thursday December 21, 2023 @07:41PM
(#38729331)
Ah.. you need a binding function to wrap the state-derived content into a reactive UI element, see: https://vanjs.org/tutorial#state-derived-child. I made the change in your code to make it work: https://jsfiddle.net/rkfmpx06/1/