types hooks

This commit is contained in:
James
2020-11-25 08:09:51 -05:00
parent 2420969375
commit 55deb3f01b
8 changed files with 43 additions and 13 deletions

View File

@@ -13,7 +13,7 @@ const RenderedFieldContext = createContext({});
export const useRenderedFields = () => useContext(RenderedFieldContext);
const RenderFields = (props) => {
const RenderFields: React.FC = (props) => {
const {
fieldSchema,
fieldTypes,

View File

@@ -1,7 +1,7 @@
import { useState, useEffect } from 'react';
// Our hook
export default function useDebounce(value, delay) {
export default function useDebounce(value: unknown, delay: number): unknown {
// State and setters for debounced value
const [debouncedValue, setDebouncedValue] = useState(value);

View File

@@ -1,8 +1,17 @@
/* eslint-disable no-shadow */
import { useEffect, useRef, useState } from 'react';
export default ({ root = null, rootMargin, threshold = 0 } = {}) => {
const [entry, updateEntry] = useState({});
type Intersect = [
setNode: React.Dispatch<Element>,
entry: IntersectionObserverEntry
]
const useIntersect = ({
root = null,
rootMargin = '0px',
threshold = 0,
} = {}): Intersect => {
const [entry, updateEntry] = useState<IntersectionObserverEntry>();
const [node, setNode] = useState(null);
const observer = useRef(
@@ -27,3 +36,5 @@ export default ({ root = null, rootMargin, threshold = 0 } = {}) => {
return [setNode, entry];
};
export default useIntersect;

View File

@@ -1,5 +1,5 @@
import { useEffect } from 'react';
const useMountEffect = func => useEffect(func, []);
const useMountEffect = (func: () => void): void => useEffect(func, []);
export default useMountEffect;

View File

@@ -3,11 +3,28 @@ import queryString from 'qs';
import { useLocale } from '../components/utilities/Locale';
import { requests } from '../api';
const usePayloadAPI = (url, options = {}) => {
type Result = [
{
isLoading: boolean
isError: boolean
data: unknown
},
{
setParams: React.Dispatch<unknown>
}
]
type Options = {
initialParams?: unknown
initialData?: unknown
}
type UsePayloadAPI = (url: string, options?: Options) => Result;
const usePayloadAPI: UsePayloadAPI = (url, options = {}) => {
const {
initialParams = {},
initialData = {},
onLoad,
} = options;
const [data, setData] = useState(initialData);
@@ -18,8 +35,8 @@ const usePayloadAPI = (url, options = {}) => {
const search = queryString.stringify({
locale,
...params,
}, { depth: 10 });
...(typeof params === 'object' ? params : {}),
});
useEffect(() => {
const fetchData = async () => {
@@ -48,7 +65,7 @@ const usePayloadAPI = (url, options = {}) => {
setIsError(false);
setIsLoading(false);
}
}, [url, locale, search, onLoad]);
}, [url, locale, search]);
return [{ data, isLoading, isError }, { setParams }];
};

View File

@@ -1,7 +1,9 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { useEffect, useRef } from 'react';
const useThrottledEffect = (callback, delay, deps = []) => {
type useThrottledEffect = (callback: React.EffectCallback, delay: number, deps: React.DependencyList) => void;
const useThrottledEffect: useThrottledEffect = (callback, delay, deps = []) => {
const lastRan = useRef(Date.now());
useEffect(

View File

@@ -1,6 +1,6 @@
import { useFormFields } from '../components/forms/Form/context';
const useTitle = (useAsTitle) => {
const useTitle = (useAsTitle: string): string => {
const { getField } = useFormFields();
const titleField = getField(useAsTitle);
return titleField?.value;

View File

@@ -1,6 +1,6 @@
import { useEffect } from 'react';
// eslint-disable-next-line react-hooks/exhaustive-deps
const useUnmountEffect = (callback) => useEffect(() => callback, []);
const useUnmountEffect = (callback: React.EffectCallback): void => useEffect(() => callback, []);
export default useUnmountEffect;