You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

react-clone-referenced-element

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-clone-referenced-element

Clones a React element while preserving its original ref

1.1.0
Source
npm
Version published
Weekly downloads
46K
13.96%
Maintainers
1
Weekly downloads
 
Created
Source

cloneReferencedElement for React

This is a version of React.cloneElement that preserves the original element's ref even if you specify a new ref for the clone.

Installation

Install this module from npm:

npm install --save react-clone-referenced-element

This library was designed for React Native, so if you are using it with React in other environments, you will need to compile the code first. How you do this is up to you. With Babel, use the following plugins:

  • es2015-block-scoping
  • object-rest-spread
  • trailing-function-commas

You will also need to transform or define a global variable named __DEV__.

Usage

The signature of cloneReferencedElement is the same as that of React.cloneElement. However, when using callback refs, it will preserve the ref on the original component if there is one.

let element =
  <Component ref={component => {
    console.log('Running the original ref handler');
  }} />
cloneReferencedElement(element, {
  ref(component) {
    console.log('Running the clone ref handler');
  },
});

When the component is mounted, the console will display:

Running the clone ref handler
Running the original ref handler

Keywords

react

FAQs

Package last updated on 12 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts