3 ms·
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 st
by seanlaff 3y ago
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))
);
};
- taoxin 3y agoYes. This is how VanJS works. Code with VanJS is often more concise compared to equivalent code with React.
- seanlaff 3y agoAh 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 https://jsfiddle.net/397fb684/ https://jsfiddle.net/397fb684/
- taoxin 3y agoAh.. you need a binding function to wrap the state-derived content into a reactive UI element, see: https://vanjs.org/tutorial#state-derived-child https://vanjs.org/tutorial#state-derived-child. I made the change in your code to make it work: https://jsfiddle.net/rkfmpx06/1/ https://jsfiddle.net/rkfmpx06/1/
- seanlaff 3y agoOh! I understand, thanks for walking through that. Yes very terse compared to the equivalent react :)