Skip to content

Instantly share code, notes, and snippets.

@ashimon83
Last active June 8, 2021 09:51
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 ashimon83/c9821d9e879f10ec6fb0562693fcda3c to your computer and use it in GitHub Desktop.
Save ashimon83/c9821d9e879f10ec6fb0562693fcda3c to your computer and use it in GitHub Desktop.
type People = {
name: string
}
const ComponentWithUsePost = ({id}) => {
const [name, setName] = useState('')
const {data, isLoading: isFetchingUser} = useFetch<User>({url: `/api/user/${id}`})
const { doPost, isLoading, hasError, errorMessage } = usePost<User>({
method: 'put',
url: '/api/user/',
})
useEffect(() => {
if (!data) return
setName(data.name)
}, [data])
if (isLoading) {
return <Loader />
}
const onClickButton = () => {
doPost({
params: {
name
},
onSuccess: (user) => {
console.log(`update ${user.name} success!`)
history.push(`/people/${user.id}`)
},
onError: (err) => {
console.log(err.message)
}
})
}
return (
<>
<input value={name} onChange={e => setName(e.target.value)} />
{hasError && <b>{errorMessage}</b>}
<button onClick={onClickButton}>edit user name</button>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment