Skip to content

Instantly share code, notes, and snippets.

@juulhao
Created March 8, 2019 19:05
Show Gist options
  • Save juulhao/f42a80fd8c28575152ae3bcd92184826 to your computer and use it in GitHub Desktop.
Save juulhao/f42a80fd8c28575152ae3bcd92184826 to your computer and use it in GitHub Desktop.
refresh fetch and caching
import React, { Component } from 'react';
import logo from './assets/images/logo.svg';
import './App.css';
import { Fetch } from 'react-data-fetching'
import api from './utils/api';
import CardClima from './components/CardClima';
class App extends Component {
constructor(props){
super(props);
this.state = {
loading: false,
cidades: []
}
}
refreshData() {
}
componentDidMount() {
this.setState({ loading: true }, () => {
api.getNairobi().then(response => {
this.setState({
loading: false,
cidades: [...this.state.cidades, response]}
)
})
api.getUbirici().then(response => {
this.setState({
loading: false,
cidades: [...this.state.cidades, response]}
)
})
api.getNuuk().then(response => {
this.setState({
loading: false,
cidades: [...this.state.cidades, response]}
)
})
});
setInterval(() => this.setState({
// logica aqui
}), 1000);
}
render() {
const {loading, cidades} = this.state
return (
<div className="App">
{
loading ? <Loading/> :
cidades.map((cidades, i) =>
<CardClima
key={i}
local={cidades.name}
temperatura={cidades.main.temp}
humidity={cidades.main.humidity}
pressure={cidades.main.pressure}
horaAtualizacao={new Date().toLocaleTimeString()}
card_name={cidades.name}
/>
)
}
</div>
);
}
}
const Loading = () => <img src={require('./assets/images/loader.svg')} alt="Carregando dados" />;
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment