Skip to content

Instantly share code, notes, and snippets.

@nervetattoo
Last active June 7, 2016 12:46
Show Gist options
  • Save nervetattoo/7a1866e69ab896210f0bda02322247a1 to your computer and use it in GitHub Desktop.
Save nervetattoo/7a1866e69ab896210f0bda02322247a1 to your computer and use it in GitHub Desktop.
HOC Api inspired by refetch and recompose for simply assigning api data as props with "middleware" for authorization
import 'withApi' from './withApi.js'
const Cat = ({cat}) => <span>{cat.name} says {cat.purr}</span>
const enhance = withApi(props => ({
cat: `/api/cat/${props.id}`
}))
export default enhance(Cat)
import Cat from './cat'
ReactDOM.render(<Cat id={12} />, element)
// <span>Fluffy says meow!</span>
import withData from 'withData'
import {connect} from 'react-redux'
import {compose} from 'redux'
const enhance = connect(
state => ({
headers: {
Authorization: `Bearer: ${token}`
}
})
)
export default compose(withData, enhance)
@nervetattoo
Copy link
Author

nervetattoo commented Jun 7, 2016

An attempt at a pair of HOCs that makes it really simple to connect a component to the result of an api endpoint.
withData is not show, but its thought to work like a stripped down version of react-refetch
Does this make sense?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment