What I am going wrong? #207
-
One of my components can not be async so it has to get the data in a traditional way. import van from "vanjs-core"
const { a, div, li, p, ul, img, span } = van.tags
export const Post = (postId) => {
const posts = van.state()
fetch(`http://localhost:5173/post/${postId}`)
.then(result => result.json())
.then(resultJson => posts.val = resultJson)
.catch(e=> posts.val= [])
return div({class: "flex text-3xl"},
posts.val ? posts.val.map(p => span("AAA")) : span("LOADING"))
} posts.val is never changing its value. Why? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
For state-derived UI elements, you need to wrap them into a binding function that returns the UI element. If a list of elements need to be rendered (in your example, Thus in your example, the return statement can be changed to: return div({class: "flex text-3xl"},
() => div(posts.val ? posts.val.map(p => span("AAA")) : span("LOADING"))) Hope it helps :-) |
Beta Was this translation helpful? Give feedback.
For state-derived UI elements, you need to wrap them into a binding function that returns the UI element. If a list of elements need to be rendered (in your example,
posts.val.map(...)
), they can be wrapped by a pass-through container (<span>
for inline elements and<div>
for block elements).Thus in your example, the return statement can be changed to:
Hope it helps :-)