Hacker News new | past | comments | ask | show | jobs | submit login

Static? That's half true.

  let planets;
  let planetFilter = planet => true;
The planets variable is static in this example, but planetFilter changes. The "Voila, it works" link near the bottom of the essay leads to a live demo: https://brlewis.github.io/2017/planets.html

Was there a particular aspect of state management you want to hear more about? I'm thinking of a follow-on post. Not sure whether the next one should be state or build system.




Oh, you're right. The state management was so subtle that it actually didn't click when I read it. I don't understand how it knows to rerender though? From what I can tell you're just setting the variable planetFilter and that's it, while with React you would have to do a setState to register it.


You write your templates to handle the state changes, like:

   m('div, {style: `display: ${status ? 'block' : 'none'}`)
Mithril's diffing engine does the rest. If status changes it knows to rerender that section.


But how does Mithril know that status changes? How does it know you just changed the variable?


By default, Mithril redraws the app after DOM events fire (in this case `onchange={filterHandler(func)}`), and when ajax requests are complete (using the `m.request()` XHR wrapper).

You can also manually trigger a redraw by using `m.redraw()`, but it is rarely needed.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: