1.0.19 • Published 10 months ago

t-react-hooks v1.0.19

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

t-hooks

This is a tiny hooks for working with react.js application.

API

Default useRequest

By default, the first parameter of useRequest is an asynchronous function, which is automatically executed when the component is initialized. At the same time, it automatically manages the status of loading, data, error of the asynchronous function.

const { data, error, loading } = useRequest(service);
const {
  loading: boolean,
  data?: TData,
  error?: Error,
  params: TParams || [],
  run: (...params: TParams) => void,
  runAsync: (...params: TParams) => Promise<TData>,
  refresh: () => void,
  refreshAsync: () => Promise<TData>,
  mutate: (data?: TData | ((oldData?: TData) => (TData | undefined))) => void,
  cancel: () => void,
} = useRequest<TData, TParams>(
  service: (...args: TParams) => Promise<TData>,
  {
    manual?: boolean,
    defaultParams?: TParams,
    onBefore?: (params: TParams) => void,
    onSuccess?: (data: TData, params: TParams) => void,
    onError?: (e: Error, params: TParams) => void,
    onFinally?: (params: TParams, data?: TData, e?: Error) => void,
  }
);

Result

PropertyDescriptionType
dataData returned by serviceTData | undefined
errorException thrown by serviceError | undefined
loadingIs the service being executedboolean
paramsAn array of parameters for the service being executed. For example, you triggered run(1, 2, 3), then params is equal to [1, 2, 3]TParams | []
run Manually trigger the execution of the service, and the parameters will be passed to the serviceAutomatic handling of exceptions, feedback through onError(...params : TParams) => void
runAsyncThe usage is the same as run, but it returns a Promise, so you need to handle the exception yourself.(...params: TParams) => Promise<TData>
refreshUse the last params, call run again() => void
refreshAsyncUse the last params, call runAsync again() => Promise<TData>
mutateMutate data directly(data?: TData / ((oldData?: TData) => (TData / undefined))) => void
cancelIgnore the current promise response() => void

Options

PropertyDescriptionTypeDefault
manual The default is false. That is, the service is automatically executed during initialization. If set to true, you need to manually call run or runAsync to trigger execution. booleanfalse
defaultParamsThe parameters passed to the service at the first default executionTParams-
onBeforeTriggered before service execution(params: TParams) => void-
onSuccessTriggered when service resolve(data: TData, params: TParams) => void-
onErrorTriggered when service reject(e: Error, params: TParams) => void-
onFinallyTriggered when service execution is complete(params: TParams, data?: TData, e?: Error) => void-

useBreakpoints

this hook is returning screen size in boolean

import {useBreakpoint} from './useBreakpoint'

const { xs, sm, md, lg, xl, xxl } = useBreakpoint();

useCreation

useCreation is the replacement for useMemo or useRef.

You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.

And similar to useRef, you can use useCreation to create some constants. But useCreation can avoid performance hazards.

const a = useRef(new Subject()); // A new Subject instance is created in every render.
const b = useCreation(() => new Subject(), []); // By using factory function, Subject is only instantiated once.

useLatest

A Hook that returns the latest value, effectively avoiding the closure problem.

Examples

Basic usage

API

const latestValueRef = useLatest<T>(value: T): MutableRefObject<T>;

useMemoizedFn

Hooks for persistent functions. In general, useMemoizedFn can be used instead of useCallback. See FAQ for special cases.

In some scenarios, we need to use useCallback to cache a function, but when the second parameter deps changes, the function will be regenerated, causing the function reference to change.

const [state, setState] = useState('');

// When the state changes, the func reference will change
const func = useCallback(() => {
  console.log(state);
}, [state]);
const memoizedFn = useMemoizedFn<T>(fn: T): T;

Result

PropertyDescriptionType
memoizedFnFunction that the reference address never changes(...args: any[]) => any

Params

PropertyDescriptionTypeDefault
fnFunction that require persistence(...args: any[]) => any-

useMount

A hook that executes a function after the component is mounted.

Examples

Default Usage

API

useMount(fn: () => void);

Params

PropertyDescriptionTypeDefault
fnThe function to be executed() => void-

useUnmount

A hook that executes the function right before the component is unmounted.

Examples

Default Usage

API

useUnmount(fn: () => void);

Params

PropertyDescriptionTypeDefault
fnThe function to be executed() => void-

useUpdate

A hook that returns a function which can be used to force the component to re-render.

Examples

Default Usage

API

const update = useUpdate();

useUpdateEffect

A hook alike useEffect but skips running the effect for the first time.

Examples

Basic usage

API

The API is exactly the same as React.useEffect.

useUpdateEffect(
  effect: React.EffectCallback,
  deps?: React.DependencyList,
)
1.0.19

10 months ago

1.0.18

10 months ago

1.0.17

10 months ago

1.0.16

11 months ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.9

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.2

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago