文档:https://tanstack.com/query/v4/docs/overview
GET请求
async function fetchData(){
return axios.get('https://api.github.com/repos/tannerlinsley/react-query').then((ree:any)=>ree.data)
}
const {data,isFetching,isError,isLoading} = useQuery(['info'],fetchData,{
// staleTime:1*60*1000,// 60秒内的重复请求会从缓存里读取值,不再进行网络请求
// cacheTime:5*60*1000,// 数据保存时间为5分钟,staleTime过期则会重新进行网络请求
// refetchInterval:10*1000,//每10秒进行一次请求
select: (result:any) => {
return result
},//对返回的数据进行进一步的操作,结果会影响data
})
// console.log('data==',data);
function getUserInfo(){
return {
id:1,
name:'jj'
}
}
const info = useQuery(['userInfo'],getUserInfo)
POST请求
function getProfile(data:any){
return new Promise((resolute,reject)=>{
resolute({
result:data.params1 + data.params2
})
})
}
const profile = useMutation((data:any)=>{
console.log('profile data=======',data);
return getProfile(data)
},{
})
<button onClick={()=>{
profile.mutate({params1:'jack',params2:'18'})
}}>pofile post</button>
网友评论