1
How do I update the status ?
How do I click register and update the screen on time, and when to click X it delete and also update the page on time. I’m not getting, I have to give an update on the manual page.
Personal API is a json file and was used json-server to simulate. Exercise also needs to use Typescript.
follows the code.
import React, { useEffect, useRef, useState } from 'react';
import './App.css';
import axios from 'axios'
function App() {
const [usuarios, setUsuarios] = useState<any>([])
const inputNome = useRef<HTMLInputElement>(null)
const inputIdade = useRef<HTMLInputElement>(null)
const inputEmpresa = useRef<HTMLInputElement>(null)
const inputTelefone = useRef<HTMLInputElement>(null)
const mostraUsuarios = () => {
axios.get("http://localhost:4000/usuarios")
.then(resposta => setUsuarios(resposta.data))
}
const enviarCadastro = () => {
const requisicao = {
name: inputNome.current?.value,
age: inputIdade.current?.value,
company: inputEmpresa.current?.value,
phone: inputTelefone.current?.value
}
axios.post("http://localhost:4000/usuarios", requisicao)
mostraUsuarios()
}
useEffect(() => {
mostraUsuarios()
}, [])
const deletarUsuario = (id: any) => {
axios.delete(`http://localhost:4000/usuarios/${id}`)
mostraUsuarios()
}
return (
<div className="App">
<input type="text" placeholder="Nome" ref={inputNome} />
<input type="text" placeholder="Idade" ref={inputIdade} />
<input type="text" placeholder="Empresa" ref={inputEmpresa} />
<input type="text" placeholder="Telefone" ref={inputTelefone} />
<button onClick={enviarCadastro}>Cadastrar</button>
<br/>
<ul>
{ usuarios !== null &&
usuarios.map((item: any) => (
<li key={item.id}>{item.name} <button onClick={() => deletarUsuario(item.id)}>X</button></li>
))
}
</ul>
</div>
);
}
export default App;
the Aces in
enviarCadastro
is running at the same time as themostrarUsuarios
needs one first then the other must be that your problem the update in hits with the cadre made?– novic
All right Novic, then I want at the time click register that it shows on the screen already the name , and when you click X already delete the user from the screen. I’m lost how to do this, if you can help me, and thanks for the tip.
– daniel
Could someone help me update the status and already show it on the screen ?. The way I did with you is only on the Google manual button.
– daniel
axios.post("http://localhost:4000/usuarios"
this return something of the back end?– novic
It’s all right, Novic. So this post is adding in the API, let’s assume you type a name and when you click on register it adds in the API, but I need it to show on the screen tb, I just can’t. The way it is in the code there I need to recharge the page on Google to appear.
– daniel
along those lines
axios.post("http://localhost:4000/usuarios", requisicao)
placeaxios.post("http://localhost:4000/usuarios", requisicao).then(() => {mostraUsuarios()})
and remove from the last linemostraUsuarios()
– novic
I tried here Novic, but it didn’t work out either.
– daniel
what happens in reality, what gives of errors? already looked at the console?
– novic