Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react axios fetchData with loading screen plus API

import axios from 'axios';
import './App.css';
import React from 'react';
class FetchData extends React.Component {
  state = {
    data: null,
    loading: true,
    error: ' '

  };
  componentDidMount() {
    axios.get(this.props.url)
    .then(res => {
      this.setState({data: res.data,
        loading: false
      });
    } )
  .catch(err => {
    console.log(err);
  });
  }
  render () {
    const {data, loading, error} = this.state
    return (
      <div>
        {loading && <p>Loading...</p>}
        {error && <p>{error}</p>}
        {data && <div>
          <h3>
            title: {data.title}</h3>
            <p>id: {data.id}</p>
            </div>}
      </div>
    )
  }

}
function App() {
  return (
    <div className="App">
      <h1>Hello React
        im a software Engineer
      </h1>
      <FetchData url="https://jsonplaceholder.typicode.com/todos/1" />
    </div>
  );
}
export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: chaine de caractère dans une autres js 
Javascript :: two dimensional array traversing in javascript 
Javascript :: add fraction in angular 
Javascript :: reset regex javascript 
Javascript :: load js on only specific page wp 
Javascript :: mouse over jest 
Javascript :: map every second character jaavascript 
Javascript :: node.js process.argv 
Javascript :: javascript non primitive data types 
Javascript :: exit node 
Javascript :: grapesjs cdn 
Javascript :: gsheet formula get last item in column 
Javascript :: index and id together angularjs 
Javascript :: javascript get first entry from set 
Javascript :: destructuring object 
Javascript :: A fatal JavaScript error has occurred. Should we send an error report 
Javascript :: how to generate random gradient javascript 
Javascript :: remove unused css and js wordpress 
Javascript :: js ternaire 
Javascript :: sleep 1 second 
Javascript :: object declaration in javascript 
Javascript :: node import json 
Javascript :: socket ERR_CONNECTION_REFUSED 
Javascript :: got bearer auth 
Javascript :: js not not 
Javascript :: js addeventlistener input search phone 
Javascript :: res.write in node js 
Javascript :: how to reload automaticaly in vue 
Javascript :: cypress/react yarn 
Javascript :: regex match between quotes without escape 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =