Created
January 1, 2021 12:18
-
-
Save JWLangford/a1fc67f056f5abc0736cc57912bd57fe to your computer and use it in GitHub Desktop.
simple api hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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