JSPM

react-clone-referenced-element

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 81833
  • Score
    100M100P100Q174857F
  • License MIT

Clones a React element while preserving its original ref

Package Exports

  • react-clone-referenced-element

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-clone-referenced-element) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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