Skip to content

Instantly share code, notes, and snippets.

@JWLangford
Created January 1, 2021 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JWLangford/a1fc67f056f5abc0736cc57912bd57fe to your computer and use it in GitHub Desktop.
Save JWLangford/a1fc67f056f5abc0736cc57912bd57fe to your computer and use it in GitHub Desktop.
simple api hook
import { useState } from "react"
import { getUser } from "../api/api"
interface User {
name: string;
email: string;
age: number;
}
export default (): [(id: string) => void, User, string] => {
const [user, setUser] = useState<User>({ name: "", email: "", age: 0 });
const [errorMsg, setErrorMsg] = useState("");
const getUserTest = async (id: string) => {
try {
const user = await getUser(`/user/${id}`);
setUser(user);
setErrorMsg("");
} catch (error) {
setErrorMsg(error);
}
};
return [getUserTest, user, errorMsg]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment