An open API service indexing awesome lists of open source software.

https://github.com/rpearce/react-with-forwarded-ref

React higher-order component (HOC) for ref forwarding
https://github.com/rpearce/react-with-forwarded-ref

forwardref hacktoberfest higher-order-component hoc react react-component

Last synced: 12 months ago
JSON representation

React higher-order component (HOC) for ref forwarding

Awesome Lists containing this project

README

          

# react-with-forwarded-ref

[![npm version](https://img.shields.io/npm/v/react-with-forwarded-ref.svg?style=flat-square)](https://www.npmjs.com/package/react-with-forwarded-ref) [![npm downloads](https://img.shields.io/npm/dm/react-with-forwarded-ref.svg?style=flat-square)](https://www.npmjs.com/package/react-with-forwarded-ref) [![bundlephobia size](https://flat.badgen.net/bundlephobia/minzip/react-with-forwarded-ref)](https://bundlephobia.com/result?p=react-with-forwarded-ref) [![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-)

This React.js [higher-order component
(HOC)](https://reactjs.org/docs/higher-order-components.html) exists to provide
an appropriate wrapper for components that utilize
[forwarded refs](https://reactjs.org/docs/forwarding-refs.html). It accepts a
`ref` via `props` and renames `ref` to `forwardedRef` along
with whatever other `props` are passed in.

## Links
* [Installation](#installation)
* [Usage](#usage)
* [All Contributors](#usage)
* [Authors](./AUTHORS)
* [Changelog](./CHANGELOG.md)
* [Contributing](./CONTRIBUTING.md)
* [Code of Conduct](./CODE_OF_CONDUCT.md)

## Installation
```
npm i react-with-forwarded-ref
```
or
```
yarn add react-with-forwarded-ref
```

## Usage
Let's say you have a react component named `Comp`:

```js
// Comp.js file

const Comp = ({ children, className }) => (


{children}

)

export default Comp
```

If you're using [`React.createRef()`](https://reactjs.org/docs/refs-and-the-dom.html#creating-refs)
and want to pass this `ref` to a child component, you need to utilize
[forwarded refs](https://reactjs.org/docs/forwarding-refs.html). This component
handles all the hassle by using the [higher-order component
(HOC)](https://reactjs.org/docs/higher-order-components.html) pattern to wrap
your component, accept the `ref`, and forward it to you as `forwardedRef`.

```js
// Comp.js file

import withForwardedRef from 'react-with-forwarded-ref'

const Comp = ({ children, className, forwardedRef }) => (


{children}

)

export default withForwardedRef(Comp)
```

## Contributors

Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):



Robert Pearce

💻 📖 💡 🤔 ⚠️

Kristián Žuffa

💻

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!