Skip to content

Commit 169fe3d

Browse files
committed
✨ 2 new hooks
1 parent 83bb909 commit 169fe3d

4 files changed

Lines changed: 86 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@crystallize/reactjs-hooks",
33
"license": "MIT",
4-
"version": "0.6.0",
4+
"version": "0.7.0",
55
"author": "Crystallize <hello@crystallize.com> (https://crystallize.com)",
66
"contributors": [
77
"Sébastien Morel <sebastien@crystallize.com>"

src/core/hooks/useFetch.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const fetchResult = async (url: RequestInfo, init?: RequestInit): Promise<any> => {
2+
const response = await fetch(url, {
3+
credentials: 'include',
4+
headers: {
5+
'Content-type': 'application/json; charset=UTF-8',
6+
Accept: 'application/json',
7+
},
8+
...init,
9+
});
10+
11+
if (!response.ok) {
12+
throw new Error(`Could not fetch ${url}. Response NOT OK.`);
13+
}
14+
const json = await response.json();
15+
if (json.errors) {
16+
throw new Error(`Could not fetch ${url}. Response contains errors.`);
17+
}
18+
return json;
19+
};
20+
21+
const jsonRequest = async <T>(input: RequestInfo, method: string, init?: RequestInit): Promise<T> => {
22+
return fetchResult(input, {
23+
method: method,
24+
...init,
25+
});
26+
};
27+
28+
export const getJson = async <T>(input: RequestInfo, init?: RequestInit): Promise<T> => jsonRequest(input, 'GET', init);
29+
30+
export const postJson = async <T>(input: RequestInfo, body: any, init?: RequestInit): Promise<T> =>
31+
jsonRequest(input, 'POST', {
32+
body: JSON.stringify(body),
33+
...init,
34+
});
35+
36+
export const useFetchResult = () => {
37+
const abortController = new AbortController();
38+
const abort = () => {
39+
abortController.abort();
40+
};
41+
42+
const signalInit = {
43+
signal: abortController.signal,
44+
};
45+
46+
const get = async <T>(url: string, init?: RequestInit): Promise<T> => {
47+
return await getJson(url, {
48+
...signalInit,
49+
...init,
50+
});
51+
};
52+
53+
const post = async <T>(url: string, body: any, init?: RequestInit): Promise<T> => {
54+
return await postJson(url, body, {
55+
...signalInit,
56+
...init,
57+
});
58+
};
59+
60+
return { get, post, abort };
61+
};

src/core/hooks/useHydrated.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useEffect, useState, ReactElement } from 'react';
2+
let hydrating = true;
3+
export function useHydrated() {
4+
let [hydrated, setHydrated] = useState(() => !hydrating);
5+
6+
useEffect(() => {
7+
hydrating = false;
8+
setHydrated(true);
9+
}, []);
10+
11+
return hydrated;
12+
}
13+
export const ClientOnly: React.FC<{ children: ReactElement<any, any>; fallback?: ReactElement<any, any> }> = ({
14+
children,
15+
fallback = null,
16+
}) => {
17+
const hydrated = useHydrated();
18+
if (hydrated) {
19+
return children;
20+
}
21+
return fallback;
22+
};

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './core/MainProvider/MainProvider';
22
export * from './core/MainProvider/Reducer';
3+
export * from './core/hooks/useFetch';
4+
export * from './core/hooks/useHydrated';

0 commit comments

Comments
 (0)