Compose HOCs imperatively like async/await. No callback hell!
In memoriam HOCs and Render Props
``js
import epitath from 'epitath'
...
const App = epitath(function*() {
const { loading, data } = yield
const { time } = yield
return (
}[![npm package][npm-badge]][npm]
Compose HOCs imperatively like async/await. No callback hell!
[npm-badge]: https://img.shields.io/npm/v/npm-package.svg?style=flat-square
[npm]: https://www.npmjs.org/package/npm-package
Install
`
yarn add epitath
`
or
`
npm install --save epitath
`Why
Render props are amazing for providing more functionality but once you need to stack a bunch of them you get what recalls a painful callback hell.`jsx
{({ data }) =>
{({ mutate, result })=>
}
}
`How
Wait, we just mentioned "callback hell". So what if we had a function that would allow us to have a kind of sugar for continuation-passing-style Γ la async/await?
And that's exactly what epitath is, it just takes care of the callbacks for you.
The whole code is this:
`js
import React from 'react'
import immutagen from 'immutagen'export default component => {
const generator = immutagen(component)
const compose = context => {
const value = context.value
return context.next
? React.cloneElement(value, null, values => compose(context.next(values)))
: value
}
function Epitath(props) {
return compose(generator(props))
}
Epitath.displayName =
EpitathContainer(${component.displayName || 'anonymous'}) return Epitath
}
`Note that epitath will only yield the first argument of the render function. In order to consume multiple arguments, we recommend creating a wrapper component:
`js
const MutationWrapper = ({ children, ...props }) =>
{(mutate, result) => children({ mutate, result })} const { mutate, result } = yield
`How is this different from Suspense?
Suspense only allows you to evalulate a promise once. It does not allow you to trigger a re-render for a state update.
And with epitath you can even use Formik, Apollo optimistic, React Powerplug and Smalldots tooling and etc!
BTW it's epitaph not "epitath"
"These Astrocoders dudes simply don't know how to spell words in English!"
Actually it was intended, for 2 reasons:
1. We wanted to put a cross as icon of the package
2. Epitaph is already taken in NPM
Contributing
$3
Install the deps
`
yarn install
`Boot the demo
`
yarn start
``- [ ] Tests
- [ ] TypeScript support
Thanks @jamiebuilds for the suggestions on how simplifying the API
Thanks goes to these wonderful people (emoji key):
|
Jamie
π€ π» |
Eli Perelman
π€ π» |
Gabriel Rubens
π€ π» |
Medson Oliveira
π€ π» |
George Lima
π€ π» |
Eliabe JΓΊnior
π» π¨ |
Guilherme Decampo
π€ |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
gtkatakura
π€ π¬ π‘ |
Erjan Kalybek
π |
Jack Hanford
π |
Haz
π |
This project follows the all-contributors specification. Contributions of any kind welcome!