Skip to content

Instantly share code, notes, and snippets.

@MaximeHeckel
Last active October 25, 2018 05: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 MaximeHeckel/f7a64f74ed731a9e500123b3a3370fb5 to your computer and use it in GitHub Desktop.
Save MaximeHeckel/f7a64f74ed731a9e500123b3a3370fb5 to your computer and use it in GitHub Desktop.
Using React without classes!
import React, { Suspense, Fragment } from "react";
import { createCache, createResource } from "simple-cache-provider";
const cache = createCache();
const createFetcher = callback => {
const resource = createResource(callback);
return () => resource.read(cache);
};
const Fetcher = createFetcher(() =>
fetch("https://jsonplaceholder.typicode.com/todos").then(r => r.json())
);
const List = () => {
const data = Fetcher();
return (
<ul>
{data.map(item => (
<li style={{ listStyle: "none" }} key={item.id}>
{item.title}
</li>
))}
</ul>
);
};
const App = () => (
<Fragment>
<h2 style={{ textAlign: "center" }}>{`React: ${React.version} Demo`}</h2>
<Suspense fallback={<div>Loading...</div>}>
<List />
</Suspense>
</Fragment>
);
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment