const [users, setUsers] = useState([]); useEffect(() => { fetch("http://localhost:5000/users") .then((res) => res.json()) .then((data) => setUsers(data)); }, []);
{users.map((user) => (
{user.name} & {user.email}
))}const handleAddUser=(e)=>{ e.preventDefault()
const name = e.target.name.value; const email = e.target.email.value; const phone = e.target.phone.value;
//object create const user = {name,email,phone}
// post data to server fetch('http://localhost:5000/user', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(user) }) .then(res => res.json()) .then(data => { const newUsers = [...users, data]; setUsers(newUsers); console.log(data) }); }