[feature] add thunking API
- Dominant language
- JavaScript
- Stars
- 688
- Forks
- 48
- PR merge metrics
- No merged PRs in 30d
Description
In `yo-yo` and `choo` it's a best practice to have element functions in an
application return the same element given the same input. This mechanism is
commonly referred to as `thunking`.
How would we feel about adding a light wrapper that exposes a `thunk` function
so that elements can be thunked with little effort. I was thinking of an API
along these lines:
``` js
// elements/my-button.js
const thunk = require('bel/thunk')
const html = require('bel')
module.exports = thunk(createMyButton)
// create a cool button that has a different color
function createMyButton (color) {
return html`
Click me! :D
`
}
```
In a `choo` app this could then be consumed as:
``` js
const html = require('choo/html')
const choo = require('choo')
const myButton = require('./elements/my-button')
const app = choo()
app.router([ '/', myView ])
app.model({
state: { color: 'blue' }
})
const tree = app.start()
document.body.appendChild(tree)
function myView (state, prev, send) {
return html`
${myButton(state.color)}
`
}
```
What do you think? Would this be a useful addition to `bel`, or should we
continue to point people to use an external thing / write it themselves? I was
thinking of including this into `choo`, but given that we strongly encourage
people to use `bel` for standalone components (yay, reusability) I figured it
might make more sense to include it here first, and then have it propagate
upwards through `yo-yo` to `choo`. Thanks!
### See Also
- https://github.com/Raynos/vdom-thunk
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.