Last active
March 7, 2018 10:44
-
-
Save tornqvist/d7e65cfe97718f19d1aa01f0877795e7 to your computer and use it in GitHub Desktop.
Choo async render issue
This file contains hidden or 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 fetch = require('node-fetch') | |
var html = require('choo/html') | |
var choo = require('choo') | |
var app = choo() | |
app.route('/', main) | |
app.use(store) | |
function main (state, emit) { | |
if (!state.data) { | |
emit('fetch:data') | |
return html`<body><h1>Loading</h1></body>` | |
} | |
return html` | |
<body> | |
<h1>Hello ${state.data}!</h1> | |
</body> | |
` | |
} | |
function store (state, emitter) { | |
emitter.on('fetch:data', function () { | |
var request = fetch('/some-api').then(function (response) { | |
return response.text().then(function (data) { | |
state.data = data // <-- reference to state is outdated as toString doesn't mutate app.state | |
emitter.emit('render') | |
}) | |
}) | |
if (state._experimental_prefetch) { | |
state._experimental_prefetch.push(request) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment