Last active
November 8, 2018 12:38
-
-
Save tfiechowski/5cc0d4991ccd5d6f33a8bf8ca6da59b2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { Component, createContext } from 'react'; | |
import PropTypes from 'prop-types'; | |
import api from './api'; | |
const { Consumer, Provider } = createContext({ | |
photos: [], | |
}); | |
export { Consumer }; | |
export class PhotosContextInner extends Component { | |
static propTypes = { | |
api: PropTypes.object, | |
children: PropTypes.any, | |
}; | |
constructor(props) { | |
super(props); | |
this.state = { | |
photos: [], | |
fetchPhotos: this.fetchPhotos, | |
}; | |
} | |
render() { | |
return <Provider value={this.state} />; | |
} | |
fetchPhotos = async () => { | |
const { data: photos } = await this.props.api.fetchPhotos(); | |
this.setState({ photos: [...this.state.photos, ...photos] }); | |
}; | |
} | |
export default function PhotosContext(props) { | |
return <PhotosContextInner api={api} {...props} />; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment