Load Data for React from a Server with fetch

Share this video with your friends

Send Tweet

We’ll fire up json-server so we can run our application against a server. We’ll take advantage of the fetch polyfill provided by create-react-app and leverage the componentDidMount lifecycle event to load our initial todo list. We’ll also add an error message to our UI in the case that the server is unavailable.

chochinlu
chochinlu
~ 7 years ago

When I ran yarn test, I got TypeError: Network request failed.

I searched stackoverflow and solved the problem by using:

import fetch from 'isomorphic-fetch'
Chris
Chris
~ 6 years ago

Unfortunately, this lesson left out a fairly important/tricky part of fetching state, which is what to do when a component unmounts while the data is being fetched. React will currently throw a console warning if you don't intentionally handle this situation. Since this is a "Production Quality" app tutorial, seems like an important thing to address.

Georgiy Nemtsov
Georgiy Nemtsov
~ 6 years ago

Absolutely agree with Chris! I hoped this would be covered. Without clearing unresolved promises before unmount the code is definitely not production ready in my opinion.

loko
loko
~ 6 years ago

what a shame :(