@bscop/use-forward-ref

React hook that permits to use the forwarded ref, or a fallback when it's not provided.

Usage no npm install needed!

<script type="module">
  import bscopUseForwardRef from 'https://cdn.skypack.dev/@bscop/use-forward-ref';
</script>

README

use-forward-ref

GitHub license npm version CircleCI Status Coverage

React hook that permits to use the forwarded ref, or a fallback when it's not provided.

Install

npm i @bscop/use-forward-ref

Usage

import React, { useRef } from "react";
import useForwardRef from "@bscop/use-forward-ref";

const Demo = React.forwardRef(
  (props, maybeRef) => {
    /**
     * maybeRef can be a ref, or nothing
     * depending by how the component is used.
     * ref is always a ref, equal to maybeRef
     * only when it is a ref.
     */
    const ref = useForwardRef(maybeRef);

    return (
      <div ref={ref}>
        Demo!
      </div>
    );
  }
);

Contribute

Read the guidelines.

Run tests

npm test

Coverage

Coverage reports are hosted on codecov.

npm run badge:coverage -- --token=<guid>

Bruno Scopelliti
www.brunoscopelliti.com