Skip to content

Commit

Permalink
Allow to reload in useQuerySuspense
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-piela committed May 20, 2019
1 parent bcdebf2 commit 2956b61
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
8 changes: 4 additions & 4 deletions docs/README.md
Expand Up @@ -370,9 +370,9 @@ const fetchUsersList = {
}; };


export const UsersListContainer = () => { export const UsersListContainer = () => {
const { payload, error } = useSuspenseQuery(fetchUsersList); const { payload, error, query } = useSuspenseQuery(fetchUsersList);


return <UsersList error={error} users={payload} />; return <UsersList error={error} users={payload} onReload={query} />;
}; };


``` ```
Expand Down Expand Up @@ -470,8 +470,8 @@ const fetchUsersList = {


export const UsersListContainer = () => ( export const UsersListContainer = () => (
<SuspenseQuery action={fetchUsersList}> <SuspenseQuery action={fetchUsersList}>
{({ error, payload }) => ( {({ error, payload, query }) => (
<UsersList error={error} users={payload} /> <UsersList error={error} users={payload} onReload={query} />
)} )}
</Query> </Query>
); );
Expand Down
Expand Up @@ -2,12 +2,21 @@ import React from 'react';
import { useSuspenseQuery } from 'react-fetching-library'; import { useSuspenseQuery } from 'react-fetching-library';


import { fetchNewsList } from '../api/actions/fetchNewsList'; import { fetchNewsList } from '../api/actions/fetchNewsList';
import { Button } from '../shared';


import { NewsList } from './NewsList'; import { NewsList } from './NewsList';
import { News } from './NewsList.types'; import { News } from './NewsList.types';


export const NewsListContainer = () => { export const NewsListContainer = () => {
const { payload, error } = useSuspenseQuery<News[]>(fetchNewsList); const { payload, error, query } = useSuspenseQuery<News[]>(fetchNewsList);


return <NewsList error={error} news={payload} />; return (
<div>
<Button onClick={query} variant="contained" color="primary">
Click to reload
</Button>

<NewsList error={error} news={payload} />
</div>
);
}; };
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{ {
"name": "react-fetching-library", "name": "react-fetching-library",
"version": "1.2.2", "version": "1.2.3",
"description": "Simple and powerful API client for react. Use hooks or FACC's to fetch data in easy way. No dependencies! Just react under the hood.", "description": "Simple and powerful API client for react. Use hooks or FACC's to fetch data in easy way. No dependencies! Just react under the hood.",
"main": "lib/index.js", "main": "lib/index.js",
"files": [ "files": [
Expand Down
19 changes: 16 additions & 3 deletions src/hooks/useSuspenseQuery/useSuspenseQuery.ts
@@ -1,4 +1,4 @@
import { useContext, useEffect, useRef } from 'react'; import { useContext, useEffect, useState } from 'react';
import { createCache } from '../../cache/cache'; import { createCache } from '../../cache/cache';
import { Action, QueryResponse } from '../../client/client.types'; import { Action, QueryResponse } from '../../client/client.types';
import { ClientContext } from '../../context/clientContext'; import { ClientContext } from '../../context/clientContext';
Expand All @@ -12,17 +12,30 @@ const cache = createCache<CacheItem>(() => true, () => true);


export const useSuspenseQuery = <T, R = any>(action: Action<R>) => { export const useSuspenseQuery = <T, R = any>(action: Action<R>) => {
const clientContext = useContext(ClientContext); const clientContext = useContext(ClientContext);
const [flag, setFlag] = useState<null | boolean>(null);
const cacheItem = cache.get(action); const cacheItem = cache.get(action);


useEffect(() => { useEffect(() => {
if (cacheItem && cacheItem.response && flag !== null) {
cache.remove(action);
}

return () => { return () => {
cache.remove(action); cache.remove(action);
}; };
}, []); }, [action]);

const forceQuery = () => {
cache.remove(action);
setFlag(!flag);
};


if (cacheItem) { if (cacheItem) {
if (cacheItem.response) { if (cacheItem.response) {
return cacheItem.response as QueryResponse<T>; return {
...(cacheItem.response as QueryResponse<T>),
query: forceQuery,
};
} }


throw cacheItem.fetch; throw cacheItem.fetch;
Expand Down

0 comments on commit 2956b61

Please sign in to comment.