choo icon indicating copy to clipboard operation
choo copied to clipboard

Route handler return value

Open NHQ opened this issue 4 years ago • 3 comments

Where in the source code does choo handle the return value of route handler functions (the hyperx html)? I see where where the router sets the handler, but I don't see where choo takes that return value and renders it.

NHQ avatar Jul 12 '21 16:07 NHQ

It's in Choo#_prerender, right here: https://github.com/choojs/choo/blob/master/index.js#L259

tornqvist avatar Jul 13 '21 12:07 tornqvist

Thanks. FWIW, I think the router auto-rendering is an anti-pattern. Everywhere else, one does the rendering manually (see beginner's guide). By returning htmlx in route functions, there is no direct way to attach logic to elements after they are appended. I use MutationObserver to listen for renders, and then make a callback out of the app emitter in my templates. Am I doing it wrong?

NHQ avatar Aug 06 '21 20:08 NHQ

Having automatic rendering on navigation I think is very much the responsibility of a framework such as choo. For reacting to updates and routing on the component level, you'd probably want to use nanocomponent (which comes with choo). It has lifecycle hooks for load/unload behavior among some other niceties. See the documentation here: https://github.com/choojs/choo#components

tornqvist avatar Aug 10 '21 08:08 tornqvist