umijs / umi-request

A request tool based on fetch.
2.21k stars 335 forks source link

[Question] why const { loading, data, error, run }= useRequest<T>(service, options); run can not work when encounting 500 #291

Closed hanalice closed 2 years ago

hanalice commented 2 years ago

import { useRequest } from "ahooks";

const { loading, data, error, run }= useRequest(service, options);

run() can successfully work, but if response.status >= 200 && response.status < 300, it will throw throw new ResponseError(copy, 'http error', body, req, 'HttpError');, and then run() can no longer work???

hanalice commented 2 years ago

@clock157 @yesmeck @yutingzhao1991

hello, anyone can help?

hanalice commented 2 years ago

I found the root cause, it's a coding problem.

I init the run function with run: () => {}, when it's a successful response it will re-value the run function to the return run of useRequest, so it can work successfully. But the data return from useRequest is null when encounting 500, so it will not change the initial definition of run function. so it does nothing as defined at the begining.