Last active
August 26, 2017 00:33
-
-
Save tttimur/f60760c675f08bba1f9ebba6ef0e7ae3 to your computer and use it in GitHub Desktop.
index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
` | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
` | |
} |
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
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.