Vlad Sabev
1 min readAug 27, 2017

--

Hey, Maxi, thanks for the comment!

Excellent point on communicating with child components! I hadn’t considered how exactly that would work out. Here’s what I came up with:

So, you can pass functions to child components —I called the function onCountChanged instead of count to avoid conflicts with the count property which holds the component’s current count. The function is accessible via vnode.attrs.onCountChanged. Since the attributes are merged with the state, you can call onCountChanged from within increment and decrement.

Alternatively, I think you could use lifecycle events (onupdate specifically) to trigger a function every time a component’s model is updated. However, thanks to your comment, I found a shortcoming in the current HyperMithril implementation— lifecycle events don’t have access to the true state of the component — the vnode.state is an empty object that doesn’t get updated when the count changes! In some ways this is good, because you can’t accidentally mutate the state from outside the component.

Still, there should be a better way to achieve what you asked for. I’ll definitely think about that and update the post, since it’s a very common use case. Thanks again for bringing this up!

--

--

Vlad Sabev
Vlad Sabev

Written by Vlad Sabev

🔧 DIY builder & web tinkerer

No responses yet