Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

aboutreact axios

// React Native Axios – To Make HTTP API call in React Native
// https://aboutreact.com/react-native-axios/

import React from 'react';
//import React in our code.
import {StyleSheet, View, TouchableOpacity, Text} from 'react-native';
//import all the components we are going to use.
import axios from 'axios';

const App = () => {
  const getDataUsingSimpleGetCall = () => {
    axios
      .get('https://jsonplaceholder.typicode.com/posts/1')
      .then(function (response) {
        // handle success
        alert(JSON.stringify(response.data));
      })
      .catch(function (error) {
        // handle error
        alert(error.message);
      })
      .finally(function () {
        // always executed
        alert('Finally called');
      });
  };

  const getDataUsingAsyncAwaitGetCall = async () => {
    try {
      const response = await axios.get(
        'https://jsonplaceholder.typicode.com/posts/1',
      );
      alert(JSON.stringify(response.data));
    } catch (error) {
      // handle error
      alert(error.message);
    }
  };

  const postDataUsingSimplePostCall = () => {
    axios
      .post('https://jsonplaceholder.typicode.com/posts', {
        title: 'foo',
        body: 'bar',
        userId: 1,
      })
      .then(function (response) {
        // handle success
        alert(JSON.stringify(response.data));
      })
      .catch(function (error) {
        // handle error
        alert(error.message);
      });
  };

  const multipleRequestsInSingleCall = () => {
    axios
      .all([
        axios
          .get('https://jsonplaceholder.typicode.com/posts/1')
          .then(function (response) {
            // handle success
            alert('Post 1 : ' + JSON.stringify(response.data));
          }),
        axios
          .get('https://jsonplaceholder.typicode.com/posts/2')
          .then(function (response) {
            // handle success
            alert('Post 2 : ' + JSON.stringify(response.data));
          }),
      ])
      .then(
        axios.spread(function (acct, perms) {
          // Both requests are now complete
          alert('Both requests are now complete');
        }),
      );
  };

  return (
    <View style={styles.container}>
      <Text style={{fontSize: 30, textAlign: 'center'}}>
        Example of Axios Networking in React Native
      </Text>
      {/*Running GET Request*/}
      <TouchableOpacity
        style={styles.buttonStyle}
        onPress={getDataUsingSimpleGetCall}>
        <Text>Simple Get Call</Text>
      </TouchableOpacity>

      <TouchableOpacity
        style={styles.buttonStyle}
        onPress={getDataUsingAsyncAwaitGetCall}>
        <Text>Get Data Using Async Await GET</Text>
      </TouchableOpacity>

      <TouchableOpacity
        style={styles.buttonStyle}
        onPress={postDataUsingSimplePostCall}>
        <Text>Post Data Using POST</Text>
      </TouchableOpacity>

      <TouchableOpacity
        style={styles.buttonStyle}
        onPress={multipleRequestsInSingleCall}>
        <Text>Multiple Concurrent Requests In Single Call</Text>
      </TouchableOpacity>

      <Text style={{textAlign: 'center', marginTop: 18}}>
        www.aboutreact.com
      </Text>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    justifyContent: 'center',
    flex: 1,
    padding: 16,
  },
  buttonStyle: {
    alignItems: 'center',
    backgroundColor: '#DDDDDD',
    padding: 10,
    width: '100%',
    marginTop: 16,
  },
});

export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript add content to array 
Javascript :: _.extend() underscore 
Javascript :: Backbone.model first parameter determines properties that each instance must have 
Javascript :: synchronous file reading 
Javascript :: Passing arrays to functions with the spread operator 
Javascript :: converting jsObject to JSON 
Javascript :: React Native Component with Random Hexa 
Javascript :: Return object in parenthesis to avoid it being considered a wrapping function body 
Javascript :: append different object in object javascript 
Javascript :: javascript Least prime factor of numbers till n 
Javascript :: telerik jquery grid trigger editcell 
Javascript :: disable submit button until form is fully validated 
Javascript :: inject html string to div javascript 
Javascript :: hsv to rgb js 
Javascript :: List content on thee currentwdr 
Javascript :: broken image 
Javascript :: onclick add and remove class using jquery 
Javascript :: angular routing appcomponent snipped 
Javascript :: javascript change favicon dynamicly 
Javascript :: knockout empty an observable array 
Javascript :: convert string to moment date 
Javascript :: how to get multiple values from json array using jq 
Javascript :: javascript split string into groups of n 
Javascript :: @typescript-eslint/no-empty-function 
Javascript :: react native class component short code 
Javascript :: copy one cell value to another in google app script 
Javascript :: nav hover add class and remove using javascript smooth 
Javascript :: pass mltiple valuesthorugh context in react 
Javascript :: angularjs checking array of objects 
Javascript :: How to set up path paramater in angular and access in the controller 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =