Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-children-clone-with-props

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-children-clone-with-props

Shorthand to clone React child components with new props added.

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
decreased by-7.69%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status NPM Package Dependency status devDependency status

react-children-clone-with-props

Shorthand to clone React child components with new props added, with the type of the children intact.

Preliminaries

If you simply React.Chlidren.map the children and clone the childs regardlessly as below, the type of the children may change.

var newChildren = React.Children.map(
    this.props.children,
    child => React.cloneElement( child, {
        propA: valueA,
        propB: valueB
    })
);

Namely, when this.props.children is a single React element, you will have newChildren as an array since React.Children.map turns a single React element into an array. Sometimes you may need the type remain intact, and you want to save the job checking against the types and returning respectively.

Install

npm install react-children-clone-with-props

Usage

// CommonJS
var ReactChildrenCloneWithProps = require( 'react-children-clone-with-props' );
// AMD
import ReactChildrenCloneWithProps from 'react-children-clone-with-props';

render() {
    var newChildren = ReactChildrenCloneWithProps( children, {
        propA: valueA,
        propB: valueB
    });

    return <div>{ newChildren }</div>;
}

If props.children is a single React element, this utility function returns a single React element. If props.children is an array of React elements, it returns an array. If there is no children, i.e. with props.children being null, it returns null. The type remains intact.

Test

npm test

License

MIT. See LICENSE.md for details.

Keywords

FAQs

Package last updated on 17 Oct 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc