- How To Connect a Backend / Database
- Fetching Data
- Sending Data
- run
npm install - run
npm run dev - create a
README.mdfile
- connect this React app with this dummy backend
- write
cd backendin the terminal - run
npm install& runnode app.jsto run the backend - add a new
availablePlacesstate with an empty array as initial state inAvailablePlaces.jsx
- use the
fetchfunction to fetch the data from the backend API - you are not allowed to use the
await&asynckeywords in a React component - call
setAvailablePlacesinside of thefetchfunction to get back the data
- use
useEffectto fix this problem of infinite loop - make the image files available with help of
app.use(express.static('images'));in the backend - send a request to for those image files to the backend in the frontend in
Places.jsx
- use
async/awaitinsideuseEffect()by creating afetchPlaces()function inAvailablePlaces.jsx - remove the old code
- show some loading text whilst we are waiting for the data to arrive in case of bad network in
AvailablePlaces.jsx - inside of
Places.jsx, accept theisLoading&loadingTextprops - use these props to show some special output
- control the
isLoadingprop dynamically depending on the progress of the HTTP request - add a new
isFetchingstate to manage the loading state when fetching the data inAvailablePlaces.jsx
- handle the error response in
AvailablePlaces.jsx- with help of the
response.okproperty throwanError& wrap the code withtry/catchto prevent the application from crashing- add an
errorstate & use it to update the UI in case of error by rendering theError.jsxcomponent
- with help of the
- replace the
Error.jsxcomponent byErrorMessage.jsxto avoid conflict with theErrorclass name
- fetch the user's location before setting the available places with help of the
navigatorobject inAvailablePlaces.jsx - sort the available places by calling
sortPlacesByDistancefromloc.js - set the
sortedPlacesas the available placessetAvailablePlacesinside of thenavigatorfunction - since you can't to use
awaitonnavigator,setIsFetchinghas to be moved elsewhere because it will be called too early- move
setIsFetching(false)into thenavigatorcallback function after setting thesetAvailablePlaces(sortedPlaces) - call it again after setting the
errorif we have one
- move
- create a helper file named
http.js& place the data fetching code fromAvailablePlaces.jsxinside of it - use this
fetchAvailablePlacesfunction inAvailablePlaces.jsx
- send the updated selection of places to the backend in
App.jsx- send the request in
handleSelectPlaceafter updating theuserPlacesstate with help of thefetchfunction - define a new
updateUserPlacesfunction in thehttp.jshelper file & call it inApp.jsx - pass the old
userPlacesstate & the newlyselectedPlacesin front of it awaitthis function & decorate thehandleSelectPlacefunction withasync- use
try/catchto wrap thisupdateUserPlacesfunction to catch potential errors
- send the request in
- select a place in the application & check the new data inserted in
data/user-places.json
- don't show a loading spinner or a loading text as you did in
AvailablePlaces.jsx - catch & handle error in
App.jsx - inform the user in case of error
- manage a new
errorUpdatingPlacesstate to update if updating your places goes wrong - show a modal to inform the user
- make the modal closable & clear the error message with help of a new
handleErrorfunction - display the modal only if there is an error to avoid bug by wrapping the
<ErrorMessage>component with a condition
- manage a new
- in
App.jsx, turn thehandleRemovePlacefunction into anasyncfunction - perform optimistic updating by first updating the
userPlacesstate - then by sending a HTTP request with help of the
updateUserPlacehelper function - add the
userPlacesstate as a dependency to theuseCallbackdependencies array - wrap this HTTP request with a
try/catch& handle the error
- fetch the places stored by the user in
App.jsxwith help ofuseEffectto avoid an inifinite loop when you update the state - in
http.js, define a new function namedfetchUserPlaces - in
App.js, call thefetchUserPlacesfunction inside ofuseEffect - catch & handle error
- manage some loading & error states
- show conditionally the error in case of error or the places in case there is no error