Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

实现useRequest #186

Open
Sunny-117 opened this issue Nov 3, 2022 · 1 comment
Open

实现useRequest #186

Sunny-117 opened this issue Nov 3, 2022 · 1 comment

Comments

@Sunny-117
Copy link
Owner

No description provided.

@yangzi0210
Copy link

yangzi0210 commented Aug 3, 2023

const useRequest = (request,options = {}){
    const {debounceWait = 0,throttleWait = 0} = options
    const [loading,setLoading] = useState(false)
    const [error,setError] = useState(null)
    const [data,setData] = useState(null)
    const run = useCallback((params)=>{
        setLoading(true)
        const doRequest = ()=>{
            request(params).then(res=>setData(res)).catch(e=>setError(e))
            .finally(setLoading(false))
        }
        if(debounceWait > 0){
            debounce(doRequest,debounceWait)
        }else if(throttleWait > 0){
            throttle(doRequest,throttleWait)
        }else{
            doRequset()
        }
    }
    ,[request,debounceWait,throttleWait])
    return {loading,run,error,data}
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants