Skip to content

Instantly share code, notes, and snippets.

@rafabarbosa
Created October 29, 2019 04:00
Show Gist options
  • Select an option

  • Save rafabarbosa/0fcf01705eaf388b4984f386e8464b9e to your computer and use it in GitHub Desktop.

Select an option

Save rafabarbosa/0fcf01705eaf388b4984f386e8464b9e to your computer and use it in GitHub Desktop.
Aprendendo a consumir API usando hooks no ReactJS
import React, { useState } from 'react';
import axios from 'axios';
export default function Lista () {
const [cep, setCep] = useState({ cep: '' });
const [informacoes, setInformacoes] = useState({
cep: '',
logradouro: '',
complemento: '',
bairro: '',
localidade: '',
uf: '',
ibge: '',
gia: ''
});
const getInformacoes = () => {
axios.get('http://viacep.com.br/ws/' + cep + '/json/')
.then(response => {
setInformacoes(response.data);
});
}
const handlingCep = (e) => {
setCep(e.target.value);
}
return (
<>
<h1>Consultando CEP</h1>
<input type="text" onChange={ (e) => { handlingCep(e) }} placeholder="Digite o CEP" />
<button onClick={ getInformacoes }>Pesquisar</button>
<ul>
<li>CEP: { informacoes['cep'] }</li>
<li>Logradouro: { informacoes['logradouro'] }</li>
<li>Complemento: { informacoes['complemento'] }</li>
<li>Bairro: { informacoes['bairro'] }</li>
<li>Localidade: { informacoes['localidade'] }</li>
<li>UF: { informacoes['uf'] }</li>
<li>IBGE: { informacoes['ibge'] }</li>
<li>GIA: { informacoes['gia'] }</li>
</ul>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment