choojs / nanocomponent

🚃 - create performant HTML components
https://leaflet.choo.io/
MIT License
366 stars 30 forks source link

this.element is null while server rendering? #83

Closed nyenye closed 6 years ago

nyenye commented 6 years ago

I've made a nanocomponent, but this.element is undefined while server rendering, and it makes bankai stop when I try to do anything with it, in my case calling getElementsByClassName().

As you can see int the screeshot, it works fine on the browser. Any ideas? Am I doing something wrong, or could this be an issue?

image

(I'm using an example from Bootstrap as template)

test-choojs.zip

bcomnes commented 6 years ago

This is expected behavior. This.element returns the DOM node mounted in the page. Since there is only a string representation during server rendering, this.element is always null in that context.

nyenye commented 6 years ago

I feared this would be the case. In that case, what could I do to keep the bankai server running? It halts when calling a function from this.element, as it is undefined.

bcomnes commented 6 years ago

You can test and guard for it by testing for this.element or use something like typeof window !== 'undefined'

nyenye commented 6 years ago

Thanks. Will close then!

bcomnes commented 6 years ago

NP good luck! Feel free to ask questions in IRC as well. Server rendering with stateful components is a tricky problem.

nyenye commented 6 years ago

Oh good to know there's an IRC channel. Other sites like stackoverflow are not very useful when using lesser known frameworks and I opted to open an issue. Thanks again!