Last active
June 7, 2016 12:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cat from './cat' | |
ReactDOM.render(<Cat id={12} />, element) | |
// <span>Fluffy says meow!</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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-refetchDoes this make sense?