Skip to content

Instantly share code, notes, and snippets.

@tfiechowski
Last active November 8, 2018 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfiechowski/5cc0d4991ccd5d6f33a8bf8ca6da59b2 to your computer and use it in GitHub Desktop.
Save tfiechowski/5cc0d4991ccd5d6f33a8bf8ca6da59b2 to your computer and use it in GitHub Desktop.
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