question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feature] add thunking API

See original GitHub issue

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:

// 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`
    <button style="background-color: ${color}">
      Click me! :D
    </button>
  `
}

In a choo app this could then be consumed as:

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`
    <main>
      ${myButton(state.color)}
    </main>
  `
}

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

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
shamacommented, Aug 3, 2016

Hmm let me give it some more thought. Initially it feels like this should be in the diffing library or yo-yo as bel doesn’t know about previous elements.

0reactions
yoshuawuytscommented, Feb 7, 2017

Yup, though I recommended nanocomponent these days - iterated on the interface a bit

On Mon, Feb 6, 2017, 23:15 Björn Roberg notifications@github.com wrote:

@yoshuawuyts https://github.com/yoshuawuyts https://github.com/yoshuawuyts/cache-element/ can be used to thunk element, or am I confused?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/shama/bel/issues/40#issuecomment-277831819, or mute the thread https://github.com/notifications/unsubscribe-auth/ACWlerG7SCfNAjUvrJ1sYvee_HIQ_er7ks5rZ5uTgaJpZM4Jbsbi .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing Logic with Thunks - Redux
The Redux Toolkit configureStore API automatically adds the thunk middleware during store creation, so it should typically be available with ...
Read more >
06 - Fetching data from an API using thunks [createAsyncThunk]
... and add our reducer to the store 06:50 How we can dispatch our thinks 09:45 Why we should use useEffect here to...
Read more >
redux-thunk/README.md at master - GitHub
Redux Thunk. Thunk middleware for Redux. It allows writing functions with logic inside that can interact with a Redux store's dispatch and getState...
Read more >
Using createAsyncThunk and the builder API to Generate ...
The key reason to use createAsyncThunk is that it generates actions for each of the different outcomes for any promised-based async call: ...
Read more >
Using Redux Toolkit's createAsyncThunk - LogRocket Blog
Learn how to use the createAsyncThunk API to perform asynchronous tasks ... passed to a Redux Thunk function — like dispatch and getState...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found