Skip to content

Commit

Permalink
update examples demo
Browse files Browse the repository at this point in the history
  • Loading branch information
yaob421123 committed Apr 26, 2023
1 parent 1de378a commit e38edcd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/mocker/mocker/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports.login = function (req, res) {
token: '5c2d6d45-ec94-319c-a9c8-cae43e192b65',
});
resolve(a);
}, 2000);
}, 500);
});
}
return res.status(401).json({
Expand Down
33 changes: 27 additions & 6 deletions examples/mocker/src/pages/Home/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useReactMutation, useReactQuery } from '@kkt/request';
import { useReactMutation, useReactQuery, queryClient } from '@kkt/request';
const Home = () => {
const data = useReactQuery({
queryKey: ['user'],
useReactQuery({
queryKey: ['users'],
url: '/api/user',
headerTokenName: 'token2',
onSuccess: (res) => {
Expand All @@ -12,18 +12,39 @@ const Home = () => {
const { isLoading, mutateAsync } = useReactMutation({
url: '/api/login',
method: 'POST',
mutationKey: ['login'],
headerTokenName: 'token2333',
onSuccess: (res) => {
queryClient.setQueryData(['login'], res);
// console.log(456, res)
},
});

const a = async () => {
const onLogin = async () => {
const result = await mutateAsync({ username: 'admin', password: 'admin' });
console.log(345, result);
};

return <div onClick={a}>登录 {JSON.stringify(isLoading)}</div>;
const onGetData = async () => {
// const data = await queryClient.getQueryData(['users']);

// 获取名为 login 的数据
// const data = await queryClient.getQueryData(['login']);
// console.log(data)

// 使名为 users 的查询失效并重新请求
// await queryClient.invalidateQueries('users');

// 删除名为 users 的缓存
await queryClient.removeQueries(['users']);
const data = await queryClient.getQueryData(['users']);
console.log(data); // undefault
};

return (
<>
<div onClick={onLogin}>登录 {JSON.stringify(isLoading)}</div>
<div onClick={onGetData}>获取登录信息</div>
</>
);
};
export default Home;

0 comments on commit e38edcd

Please sign in to comment.