| Start |
|---|
| Using Promises with the fetch API and Redux Redux is the most common state management library used with React-Native. The following example demonstrates how to use the fetch API and dispatch changes to your applications state reducer using redux-thunk. |
| HTTP with the fetch API It should be noted that Fetch does not support progress callbacks. See: https://github.com/github/fetch/issues/89 (opens new window). The alternative is to use XMLHttpRequest https://developer.mozilla.org/en-US/docs/Web/Events/progress (opens new window). More details about fetch can be found at MDN (opens new window) |
Networking with XMLHttpRequest |
WebSockets |
| Http with axios Configure For web request you can also use library axios (opens new window). It's easy to configure. For this purpose you can create file axios.js for example: and then use it in any file you want. Requests To avoid using pattern 'Swiss knife' for every service on your backend you can create separate file with methods for this within folder for integration functionality: Testing There is a special lib for testing axios: axios-mock-adapter (opens new window). With this lib you can set to axios any responce you want for testing it. Also you can configure some special errors for your axois'es methods. You can add it to your axios.js file created in prevous step: for example. Redux Store Sometimes you need to add to headers authorize token, that you probably store in your redux store. In this case you'll need another file, interceptors.js with this function: Next in constructor of your root component you can add this: and then all your requests will be followed with your authorization token. As you can see axios is very simple, configurable and useful library for applications based on react-native. |
| Web Socket with Socket.io Install socket.io-client Import module Initialize in your constructor Now in order to use your socket connection properly, you should bind your functions in constructor too. Let's assume that we have to build a simple application, which will send a ping to a server via socket after every 5 seconds (consider this as ping), and then the application will get a reply from the server. To do so, let's first create these two functions: Now, we need to bind these two functions in our constructor: After that, we also need to link _getReply function with the socket in order to receive the message from the socket server. To do this we need to attach our _getReply function with socket object. Add the following line to our constructor: Now, whenever socket server emits with the 'dong' your application will able to receive it. Syntax
Remarks
|
Read Article
https://develop.horje.com/learn/1434/reference