Axios - Fetching data from remote API on the client-side with React

Veröffentlicht am: 13. April 2020

Lesedauer: 1 Min.

Kategorien:

npm i axios

import React from 'react'
import axios from 'axios'

const ComponentName = () => {

  let [posts, setPosts] = React.useState([])

  React.useEffect(() => {
      axios
      .get('https://jsonplaceholder.typicode.com/posts')
      .then(response => {
        console.log(response)
        setPosts(response.data)
      })
      .catch(error => console.log(error))
  }, [])



  return (
    <React.Fragment>
        <ul>
          {posts.map(post => (
            <React.Fragment>
              <p>{post.id}</p>
              <h2>{post.title}</h2>
            </React.Fragment>
          ))}
        </ul>
    </React.Fragment>
  )
}

export default ComponentName

Articles in related Categories

  • Default props in React and Gatsby
  • 16. April 2020

  • React Redux State Management
  • 07. April 2020

  • React Context
  • 13. April 2020

    Google Tag Manager

    Axios

    Redux

    Express

    Node.js

    Git

    HTML

    GraphQL

    Javascript

    styled-components

    Material UI

    CSS

    Gatsby

    React