Redux has two powerful libraries that are designed to work with async API calls. These allow you to handle retries, errors, and slow API calls in the background of your app, simply by listening for a specific action.
Redux-thunk or Redux-saga are what you are probably going to want to use, so that you do not have to do all of the work of managing how components deal with API calls. Strongly recommend you check these out - there are quite a few tutorials out there that describe how to use these modules. Doing this by hand is not a good best practice IMO.
Answer from manbearshark on Stack OverflowBest API Call Methodology
react native - Where should I put API calls? - Stack Overflow
Looking for ideas for react project
Coming up with project ideas is tricky sometimes. I usually try to think about apps I've used and try to replicate them, or create one with a slight twist. Or think about hobbies you have and make a little utility app for that. If your goal is to practice, anything is fair game. IMO originality is the enemy if you want to practice a skill.
I wrote up a post with some ideas on how to come up with your own projects (has some examples too) and another one with a few project ideas ranging from simple to more complex.
More on reddit.comData Fetching Best Practices
Videos
Redux has two powerful libraries that are designed to work with async API calls. These allow you to handle retries, errors, and slow API calls in the background of your app, simply by listening for a specific action.
Redux-thunk or Redux-saga are what you are probably going to want to use, so that you do not have to do all of the work of managing how components deal with API calls. Strongly recommend you check these out - there are quite a few tutorials out there that describe how to use these modules. Doing this by hand is not a good best practice IMO.
For api handling. you can have one single file which will have all function (export) and other settings needed so that you just have to import required methods.
You can use starter kit for react-native for basic structure for example: https://github.com/futurice/pepperoni-app-kit
it provides most of the things that we need for fresh project setup.
Hey all,
I need to fetch some data from a public API and firebase, and I got it working with the default react native "fetch" method. After doing some research online, there are clearly other methods of doing this, and wondering if anyone has some good thoughts on why you would choose one of the below vs. another:
-
The provided Fetch API
-
Axios
-
Other options?
Thanks for your thoughts!
You're on the right track. The most common mistake is to put your API calls in the componentWillMount lifecycle hook, but you're smarter than most people :)
As with most things, the correct answer though is going to be a dissatisfying "it depends". As you're just getting started, I think you're on the right track though. Keep it simple until simple becomes difficult to manage, then refactor to solve for that complexity.
There are several strategies you can employ. I think you've started with the right "first step". Here are a couple other strategies you might want to investigate though when the time is right.
Container Components
You're basically wrapping your stateless components in a container responsible for fetching the needed data. This would be your logical "next step". Here's a few articles I have bookmarked on the topic.
- https://css-tricks.com/learning-react-container-components/
- https://voice.kadira.io/let-s-compose-some-react-containers-3b91b6d9b7c8#.qgg1i13vp
- http://krasimirtsonev.com/blog/article/react-js-presentational-container-components
API Helper
This is a strategy I learned from Tyler McGinnis and used on one of my first projects. You'd basically put all your API calls into a single, helper file. When you need to make a call out to an API, you just import it and use it. You could easily combine this with the Container strategy above if you wanted.
- https://github.com/geirman/RepairMaps/tree/master/App/Utils
Redux
Redux should be your last stop. This adds a lot of boilerplate to the project and comes at a cost. Keep it simple until you need to solve the problems Redux is really good at solving. How do you know when that is? Dan Abromov wrote a great article on it, so I'll let him tell you so you can make the trade offs yourself.
- https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367
You could also look into mobx . You can place your API calls in mobx @action methods