Created
March 30, 2018 10:29
-
-
Save antoinejaussoin/34267f5ada779986f90e144e5a9542c5 to your computer and use it in GitHub Desktop.
Consuming an API on a React component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import styled from 'styled-components'; | |
import Card from './Components/Card'; | |
const Container = styled.div` | |
display: flex; | |
margin: 30px; | |
> * { | |
margin-right: 20px; | |
} | |
`; | |
class App extends Component { | |
state = { cards: [] }; | |
componentDidMount() { | |
fetch('/api/cards') | |
.then(response => response.json()) | |
.then(cards => this.setState({ cards })); | |
} | |
render() { | |
return ( | |
<Container> | |
{ this.state.cards.map(card => <Card color={card.color}>{card.label}</Card>) } | |
</Container> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment