Skip to content

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

License

Notifications You must be signed in to change notification settings

brunoscopelliti/use-forward-ref

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks