1.3.3 • Published 5 months ago
@banyudu/use-service v1.3.3
@banyudu/use-service
useSWR with custom fetcher
useService buid on top of useSWR, with custom fetcher and validate function
Install
npm install @banyudu/use-serviceUsage
// useXxxx.ts
import useService from '@banyudu/use-service'
const fetcher = async (params: XxxxReq): Promise<XxxxResp> => {
return axios.post('/api/xxxx', params)
}
const useXxxx = useService(fetcher, params => Boolean(params.id)) // fetcher will not be called if params.id is falsy
export default useXxxx// app.tsx
import useXxxx from '@hooks/useXxxx'
const App = () => {
const { data: xxxxRes, isValidating: xxxxLoading, wait: waitXxxx } = useXxxx({ id: 1 })
return (
<div>
{xxxxLoading ? 'loading' : xxxxRes?.data}
</div>
)
}useXxxx will automatically send request when params change, if you want to manually trigger request, you can pass refreshFlag as second param, like:
const App = () => {
const [refreshFlag, setRefreshFlag] = useState(Math.random())
const { data: xxxxRes, isValidating: xxxxLoading } = useXxxx({ id: 1 }, refreshFlag)
}1.3.3
5 months ago
1.3.2
5 months ago
1.3.1
11 months ago
1.3.0
12 months ago
1.2.2
1 year ago
1.2.1
2 years ago
1.2.0
2 years ago
1.1.0
2 years ago
1.0.5
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago
0.1.4
2 years ago
0.1.3
2 years ago
0.1.2
2 years ago
0.1.1
2 years ago
0.1.0
2 years ago