All Projects → Astrocoders → Epitath

Astrocoders / Epitath

Licence: mit
Compose render props imperatively with async/await/CPS kinda sugar

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Epitath

stickyard
Make your React component sticky the easy way
Stars: ✭ 83 (-78.88%)
Mutual labels:  render-props
redux-render
Ergonomic Redux bindings for React using render functions
Stars: ✭ 54 (-86.26%)
Mutual labels:  render-props
react-sheltr
Shared element transition helper components for React
Stars: ✭ 33 (-91.6%)
Mutual labels:  render-props
react-save-localstorage
🗄 React component to save data to localStorage on render phase safely
Stars: ✭ 26 (-93.38%)
Mutual labels:  render-props
repromised
🤝 Declarative promise resolver as a render props component
Stars: ✭ 20 (-94.91%)
Mutual labels:  render-props
react-virtual-container
Optimise your React apps by only rendering components when in proximity to the viewport.
Stars: ✭ 52 (-86.77%)
Mutual labels:  render-props
reason-epitath
CPS sugar usage for React Render Props composition in ReasonML
Stars: ✭ 16 (-95.93%)
Mutual labels:  render-props
React Wasm
Declarative WebAssembly instantiation for React
Stars: ✭ 349 (-11.2%)
Mutual labels:  render-props
react-immer
No nonsense state management with Immer and React hooks
Stars: ✭ 13 (-96.69%)
Mutual labels:  render-props
react-treefold
A renderless tree component for your hierarchical React views
Stars: ✭ 37 (-90.59%)
Mutual labels:  render-props
paginated
⚛️ React render props component & custom hook for pagination.
Stars: ✭ 20 (-94.91%)
Mutual labels:  render-props
react-combine-contexts
🚀Use multiple React Contexts without pain.
Stars: ✭ 23 (-94.15%)
Mutual labels:  render-props
squeezy
1 kB React component for accessible accordions / collapse UI
Stars: ✭ 31 (-92.11%)
Mutual labels:  render-props
redebounce
↘️ Render Props component to debounce the given value
Stars: ✭ 14 (-96.44%)
Mutual labels:  render-props
react-native-google-autocomplete
A react-native component with render props around the Google Autocomplete Api
Stars: ✭ 103 (-73.79%)
Mutual labels:  render-props
render-props
㸚 Easy-to-use React state containers which utilize the render props (function as child) pattern
Stars: ✭ 33 (-91.6%)
Mutual labels:  render-props
react-gearbox
⚙️📦 Gearbox - Renderless state provisioning and composition
Stars: ✭ 31 (-92.11%)
Mutual labels:  render-props
React Fns
Browser API's turned into declarative React components and HoC's
Stars: ✭ 3,734 (+850.13%)
Mutual labels:  render-props
react-patterns
react patterns examples
Stars: ✭ 39 (-90.08%)
Mutual labels:  render-props
render-props
Easily and reliably support Render Props, Component Injection, and Function as a Child
Stars: ✭ 82 (-79.13%)
Mutual labels:  render-props

epita✞h

All Contributors

In memoriam HOCs and Render Props

Read the article

Also, we think you may want to take a look on React Hooks API now

import epitath from 'epitath'
...

const App = epitath(function*() {
  const { loading, data } = yield <Query />
  const { time } = yield <Time />

  return (
    <div className="App">
      {loading ? (
        <h1>Loading</h1>
      ) : (
        <div>
          <h1>{`Hello, ${data.user.name}`}</h1>
          <h2>The time is {time.toLocaleString()}!</h2>
        </div>
      )}
    </div>
  )
})

npm package

Compose HOCs imperatively like async/await. No callback hell!

Live demo Source of demo

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.

<Query>
  {({ data }) =>
    <Mutation>
      {({ mutate, result })=>
        <Form>
        etc
        </Form>
      }
    </Mutation>
  }
</Query>

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:

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:

const MutationWrapper = ({ children, ...props }) =>
  <Mutation {...props}>{(mutate, result) => children({ mutate, result })}</Mutation>

const { mutate, result } = yield <MutationWrapper />

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

Steps to get it running

Install the deps

yarn install

Boot the demo

yarn start

Things missing that we would like a little help

  • [ ] Tests
  • [ ] TypeScript support

Acknowledgements

Thanks @jamiebuilds for the suggestions on how simplifying the API

Contributors

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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].