项目作者: benpptung

项目描述 :
return children with new props merged
高级语言: JavaScript
项目地址: git://github.com/benpptung/react-children-with.git
创建时间: 2017-05-28T08:32:28Z
项目社区:https://github.com/benpptung/react-children-with

开源协议:

下载


react-children-with

send new props to children in React.

Installation

npm install -S react-children-with

Example

Instead of

  1. const React = require('react');
  2. var children = React.Children.map(this.props.children, child=> {
  3. return React.cloneElement(child, new_props);
  4. })

Just write

  1. const merge = require('react-children-with');
  2. var children = merge(this.props.children, new_props);

or Instead of

  1. const React = require('react');
  2. var children = React.Children.map(this.props.children, child=> {
  3. return [Apple, Orange].indexOf(child.type) >= 0 ?
  4. React.cloneElement(child, new_props) : child;
  5. })

just write

  1. const merge = require('react-children-with');
  2. var children = merge(this.props.children, new_props, [Apple, Orange]);

merge.deep()

same as merge(), but it can merge props to grand child, or grand’s grand child….

  1. <Parent>
  2. <Child>
  3. <GrandChild ></GrandChild>
  4. </Child>
  5. <Child>
  6. <GrandChild ></GrandChild>
  7. </Child>
  8. </Parent>

in Parent, merge new_props to GrandChild and skip Child

  1. const merge = require('react-children-with').deep;
  2. var children = merge(props.children, {onSelect}, [GrandChild]);