Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active September 5, 2018 22:02
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 ryanflorence/28a172649a39adf776257fff177dafd0 to your computer and use it in GitHub Desktop.
Save ryanflorence/28a172649a39adf776257fff177dafd0 to your computer and use it in GitHub Desktop.
import React from 'react'
import Component from "@reach/component-component"
const Fetch = ({ url, props }) => (
<Component
url={url}
initialState={{ data: null }}
didMount={({ setState }) => {
fetch(url)
.then(res => res.json())
.then(data => setState({ data }));
}}
didUpdate={({ prevProps, setState }) => {
if (prevProps.url !== url) {
fetch(url)
.then(res => res.json())
.then(data => setState({ data }));
}
}}
{...props}
/>
)
export default Fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment