useRefWithInitialValueFactory

    API

    import useRefWithInitialValueFactory from '@restart/hooks/useRefWithInitialValueFactory'
    • useRefWithInitialValueFactory<T>(initialValueFactory: () => T) => MutableRefObject<T>

      Exactly the same as useRef except that the initial value is set via a factroy function. Useful when the default is relatively costly to construct.

      const ref = useRefWithInitialValueFactory<ExpensiveValue>(() => constructExpensiveValue())

      Parameters

      • initialValueFactory() => T

        A factory function returning the ref's default value

      Return Value MutableRefObject<T>