Make professional mobile apps with React Native and Typescript — Fetch APIs (Chapter 3 — Part 4)

Photo by Adeolu Eletu on Unsplash
git clone -b integrate_3rd_components

Pro tip: It's used for learning so we store the API key directly in our code. However, for a production application, due to security reasons, in this case we should store it in our backend system and expose our own APIs as a proxy to call OpenWeatherMap APIs then call our APIs from our app.

Pro tip: It's a best practice to store all configuration data in a central place so that we can manage and modify them if needed easily. Store it in a ts/json makes IntelliSense available so it's can be referenced automatically from the code.

yarn add dayjs

Prop tip: store common functions in helpers so they can be reused in the project.

weather services

Pro tip: we can set default values to parameters as we do to parameters unit & lang.

Pro tip: if the number of parameters is less than 3, we can define the function as const func = funcA(para1: string, para2: string) . If not, we should only define 1 object parameter and pass all data as a JSON object const func = funcA(para: {para1: string; para2: string}) . It makes updating easier in the future without worrrying about their orders.

Pro tip: put the logic of retrieving data and formatting it in a separate service make the component cleaner and a preferable way to separate the logic. The result can be reconstructed and formatted into a new model to best fit our app, not always the same structure as the API result.

Pro tip: React Native uses the same Fetch API as the browser. You can learn more about it here.

weather screen
weather screenshots

Pro tip: Break a big screen into smaller components.

yarn add immer use-immer
const toggleUnit = (): void => {   setConditions((draft) => {      draft.unit =         conditions.unit ===         ?         :;   });};

Pro tip: if the state is a primitive value, use useState. Otherwise, use useImmer.

yarn add react-native-snap-carousel
yarn add --dev @types/react-native-snap-carousel
yarn add react-native-fast-image
update components.
core/components/index.ts & core/helpers/format_date.ts
final result
git clone -b fetch_api



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store