87
React Native - Beautiful Horoscope App
Recently I was working on sample project that helps to sharpen my skill in React Native (RN), and I found an interesting API which related to horoscope. It was Aztro API, an open-source horoscope API. Hence, I decided to write myself a Horoscope App!
![](https://res.cloudinary.com/practicaldev/image/fetch/s--zWIjg1sj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2APOgx2ZnJ4a-BMgcKK9HJMA.jpeg)
React Native (of-course!) and React Hooks
Redux & Redux-Saga
Axios
Because of my company projects are using Redux and Redux-Saga, and I am still very weak on the concept, hence I decided to make the app with Redux and Redux-Saga as the app’s state management.
Redux is one of the most popular state management framework for React application and Redux Saga helps to manage the side effect asynchronously. You may learn more on Redux and Redux Saga. (I am still beginner on these! 😃). Their explanation are pretty clear but real-life experience is always better than reading book!
Back to API, It was a POST request with 2 params, sign and day .
POST: https://aztro.sameerkumar.website?sign= <sign> &day= <day>
<!-- RESPONSE -->
{"current_date": "November 1, 2021", "compatibility": " Cancer", "lucky_time": " 7am",
"lucky_number": " 64", "color": " Spring Green", "date_range": "Mar 21 - Apr 20",
"mood": " Relaxed", "description": "It's finally time for you to think about just
one thing: what makes you happy. "}
Here is the axios POST request implementation.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--7QhsjneR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2216/1%2Am30F7VMDZh_fOB5J1AE4hA.png)
That’s it! Simple API call right? but how can we integrate it into Redux?
There are 3 important parts: Actions, Reducers & Store
- Actions
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Zb4hj5ZB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ADSGzWF-2TpVJgPQN_sqsFw.png)
As the API requested, there will 2 be request params sign & day and we created a type for it, API_REQUEST
2. Reducers
![](https://res.cloudinary.com/practicaldev/image/fetch/s--mnWW922V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2360/1%2ADx20vMDv6sjqZcQ8VcJKYw.png)
First, we defined our initial state where it contains loading , error & data . Then, we created a HoroscopeReducer that take current state, perform an action and return it with a new state. In the snippets, when we requesting from API, loading is set to true. If successfully get response, loading is set to false and data is set to response’s data. Meanwhile if error happens, error will be set to response’s error.
3. Store
Store is the place where hold the applicate state. Since horoscopeRequest is an async function, we can use Redux Saga to handle these operation.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--gjb20-A3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2332/1%2ACYs1TWMVzSoPyocM1a5zVg.png)
We created a Saga watcher in generator function getHoroscope based on API_REQUEST actions and takeLatest will only take care of the latest sagas and automatically cancel any previous on-going sagas.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Xse7810i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AvRwyRV8vzGnoN3XzJQiqAQ.png)
I had created a rootSaga in case there is any further sagas that can be merge into a single entry point for sagaMiddleware to run.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--sr2CHNUs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2160/1%2AgORNNY-7zmMcdoJiAI8e7w.png)
Finally, we create store with sagaMiddleware. Then run our rootSaga and render our application.
After having store, we need a provider to bind our store to our application.
![](https://res.cloudinary.com/practicaldev/image/fetch/s---RLr6mgp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2188/1%2ApUiON_Ar6M3GLKSjqG4wEA.png)
We are almost there! But how can we call our API in UI?
![](https://res.cloudinary.com/practicaldev/image/fetch/s--ZX1Nma2V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2884/1%2AhEzTdFMJkZfJBIR9vllb8A.png)
Since we only need to request API when selected a horoscope, we will request it in useEffect() (when it first launched the page & changing date). dispatch is used for request API.
Then, mapStateToProps is needed to map our state’s data into props, and connect mapStateToProps to our HoroscopeDetail .
Finally, we can get our state in HoroscopeDetail ‘s props — horoscopeModel !
There is 2 page in this application, which is Home & HoroscopeDetail .
In Home , I had created a FlatList to render horoscopeList (array of 12 zodiac) with customised HoroscopeCard .
![](https://res.cloudinary.com/practicaldev/image/fetch/s--WYt9xJsb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3000/1%2ArolfPVQSP_0A-gJ2dl_M3Q.png)
![](https://res.cloudinary.com/practicaldev/image/fetch/s--ddnvTbPE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ACnQYebTC2oZnAOVPC-H_Mg.png)
In HoroscopeDetail , all the components are wrapped into SafeAreaView and ScrollView . Figure below shows the overall of the components used.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Fz1hGlSl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AhhuAvEqCGO-fZAQ5HZWCEA.png)
![](https://res.cloudinary.com/practicaldev/image/fetch/s--c2NZjh19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3344/1%2AkTZl-sLkJhdFtwCUWZl7Dg.png)
iOS
![](https://res.cloudinary.com/practicaldev/image/fetch/s--oNgZNraB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Az8oRGOEUNKA1aZbsTuT4KA.gif)
Android
![](https://res.cloudinary.com/practicaldev/image/fetch/s--MtGlhp6W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AiEVsrrUa-sBhfvvDuF1nZw.gif)
That’s all from me! Hope your guys enjoy reading my article and love what I had done. However, there is still some features to be enhance.
Shared element transition for zodiac icon.
Feature to share horoscope to your friends.
AppWidgets to show today’s horoscope.
And plenty more..
I had built a testing Android APK for you guys to download and try it out (check my Github link on bottom of the article)! Do drop me any feedback and please correct me if I have done anything wrong in this project. Happy React-Native-ing! 🍷
87