Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@bscop/use-forward-ref

brunoscopelliti638MIT1.0.2TypeScript support: included

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

react, react hook, react forwardRef, conditional ref, react hook ref, react useRef, useForwardRef

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

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.2 (2021-06-01)

Bug Fixes

1.0.1 (2021-05-30)

Bug Fixes

  • change package name (7092f7e)
  • changes remaining references of old/wrong name (b3d28a1)

1.0.0 (2021-05-30)

Features