Skip to content

Instantly share code, notes, and snippets.

@antoinejaussoin
Created March 30, 2018 10:29
Show Gist options
  • Save antoinejaussoin/34267f5ada779986f90e144e5a9542c5 to your computer and use it in GitHub Desktop.
Save antoinejaussoin/34267f5ada779986f90e144e5a9542c5 to your computer and use it in GitHub Desktop.
Consuming an API on a React component
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