-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question about lifecycle, internal component state #50
Comments
This is a common question. We should add an example to the README. If you are handling events internally and need to trigger re-renders, you can just call var Nanocomponent = require('nanocomponent')
var html = require('bel')
var SomeEventEmitterThing = require('foo')
class Component extends Nanocomponent {
constructor () {
super()
this.arguments = []
this.bus = new SomeEventEmitterThing()
this.handleInternalUpdates = this.handleInternalUpdates.bind(this)
}
handleInternalUpdates (someNewArg) {
// re-render with old arguments
this.render.apply(this.arguments)
}
createElement (foo, bar, baz) {
this.arguments = arguments
return html`
<div>${foo} ${bar} ${baz}</div>
`
}
update (newColor) {
return false
}
load {
this.bus.on('event', this.handleInternalUpdates)
}
unload {
// usually there is no point to keep running render on unmounted components
this.bus.removeListener('event', this.handleInternalUpdates)
}
} |
Yeah—an example in the |
Great, thanks! I did see the mention about caching internal args, but coming from React it scared me. That being said, I don't know enough about React internals and perhaps they do the same thing for purposes of comparing props. Will see about submitting a PR 😄 |
Agreed! One idea is that we auto-cache arguments and let you do something built in along the lines of this.bus.emit('render') and then the render function will get last args and whatever internal state you have changed. React does a lot of this stuff automatically, and one area this was exploring was not making these assumptions for people to cut down on API size and allow more freedom/creativity, but this is common enough to shortcut for people maybe. |
Definitely. I think having the |
I think this idea might fit with #44 |
Hello,
I'm wondering about how one would trigger a render from from inside a component mounted inside a choo app.
I'm referencing your leaflet component and the lifecycle diagram, and want to take a swing at a google maps one, but I'm having trouble figuring out how to emulate React's
setState
. Choo is cool because you have absolute control about when to rerender, but I can't wrap my head around how that gels with the ability of a component to trigger it's own render based onsetState
.It seems that if I want
render
to be called on my component, I would have to pass in theemit
fn? Is that the case, or is there a way I can force a render without any upstream dependencies?Thanks for any help you could provide!
The text was updated successfully, but these errors were encountered: