Skip to content

Instantly share code, notes, and snippets.

@tttimur
Last active August 26, 2017 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tttimur/f60760c675f08bba1f9ebba6ef0e7ae3 to your computer and use it in GitHub Desktop.
Save tttimur/f60760c675f08bba1f9ebba6ef0e7ae3 to your computer and use it in GitHub Desktop.
index.js
var Nanocomponent = require('nanocomponent')
var html = require('choo/html')
class Header extends Nanocomponent {
constructor () {
super ()
this.state = null
this.emit = null
}
createElement(state, emit, black='') {
console.log('render')
this.state = state
this.emit = emit
return html`
<header class='${black} psf z4 x xjb w100 px2 py1'>
header
</header>
`
}
update (newState, emit) {
if (emit != this.emit) {
this.state = newState
this.emit = emit
return true
} else {
return false
}
}
}
module.exports = Header
var html = require('choo/html')
var Header = require('./header.js')
var header = new Header()
module.exports = function (state, emitter) {
return html`
<main class="ffsans">
${header.render(state, emitter)}
Home
</main>
`
}
var choo = require('choo')
var html = require('choo/html')
var home = require('./home.js')
var post = require('./post-view.js')
app.route('/', home)
app.route('/*', post)
var tree = app.start()
document.body.appendChild(tree)
var html = require('choo/html')
var Header = require('./header.js')
var header = new Header()
module.exports = function (state, emitter) {
return html`
<main class="ffsans">
${header.render(state, emitter)}
Post
</main>
`
}
@bcomnes
Copy link

bcomnes commented Aug 26, 2017

Did you figure it out? You can crate one instance and share it between views, or you can create an instance for each view.

https://github.com/hypermodules/hyperamp/blob/master/renderer/pages/main/index.js
https://github.com/hypermodules/hyperamp/blob/master/renderer/pages/preferences/index.js

It should work either way. The only restriction on modules is that you shouldn't use an instance in more on than once place at a time in the DOM.

@bcomnes
Copy link

bcomnes commented Aug 26, 2017

Also you probably want to update based on state, not emitter. The emitter doesn't change between renders iirc. Neither does the state object instance. You should be comparing data and not top level object references.

https://gist.github.com/siiiick/f60760c675f08bba1f9ebba6ef0e7ae3#file-header-js-L24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment