react-deep-map

A utility function for deeply transforming React element trees.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
react-deep-map
101.0.08 years ago8 years agoMinified + gzip package size for react-deep-map in KB

Readme

react-deep-map
Deeply transform a React element tree.

Example

npm install react-deep-map
import React from "react";
import reactDeepMap from "react-deep-map";

// replace all instances of "&" in classNames with `blockName`
function amp(blockName, tree) {
  return reactDeepMap(tree, (el) => {
    if(el && el.props && el.props.className && el.props.className.indexOf("&") !== -1) {
      return React.cloneElement(el, {
        className: el.props.className.replace("&", blockName)
      });
    } else {
      return el;
    }
  });
}

export default class Thing extends React.Component {
  render() {
    return amp("Thing",
      <div className="&">
        <h1 className="&__title">{this.props.title}</h1>
        <button className="&__btn">Click Me</button>
      </div>
    );
  }
}