Options
All
  • Public
  • Public/Protected
  • All
Menu

@aonawale/react-hooks

Index

Functions

Const useComputedState

  • useComputedState<T>(factory: () => T, dependecies: undefined | DependencyList): [T, Dispatch<T>]
  • Returns a computable stateful value, and a function to update it. useComputedState will only recompute the state when one of the dependecies has changed. If no dependecies array is provided, a new value will be computed on every render.

    example
    import {useComputedState} from 'react-hooks'

    const Component = ({defaultName}: {defaultName: string}) => {
    const [name, setName] = useComputedState(() => defaultName, [defaultName])

    const handleChange = (event) => setName(event.target.value)

    return (
    <>
    {name}
    <input onChange={handleChange} value={name} />
    </>
    )
    }

    Type parameters

    • T

    Parameters

    • factory: () => T

      A function that computes and returns the next state.

        • (): T
        • Returns T

    • dependecies: undefined | DependencyList

      An optional array of dependecies that triggers the computation of the next state.

    Returns [T, Dispatch<T>]

Const usePromise

  • usePromise<T>(promissory: () => Promise<T>, dependecies: DependencyList): State<T>
  • React hook to simplify the use of asynchronous operations.

    example
    import {usePromise} from 'react-hooks'

    const Component = () => {
    const {data} = usePromise(async () => 1, [])

    return <>{data.toString()}</>
    }

    Type parameters

    • T

    Parameters

    • promissory: () => Promise<T>

      A function that returns a promise.

        • (): Promise<T>
        • Returns Promise<T>

    • dependecies: DependencyList

    Returns State<T>

Const usePromiseEffect

  • usePromiseEffect<T>(promissory: () => Promise<T>, callback: (error: undefined | Error, data: undefined | T) => void, dependecies: DependencyList): void
  • Type parameters

    • T

    Parameters

    • promissory: () => Promise<T>
        • (): Promise<T>
        • Returns Promise<T>

    • callback: (error: undefined | Error, data: undefined | T) => void
        • (error: undefined | Error, data: undefined | T): void
        • Parameters

          • error: undefined | Error
          • data: undefined | T

          Returns void

    • dependecies: DependencyList

    Returns void

Generated using TypeDoc