GithubHelp home page GithubHelp logo

map-props-to-children's Introduction

map-props-to-children

Build Status

Map injected React props from Higher Order Components to child components.

This is best used as an enhancement to the Redux Container Pattern.

Installing

yarn add map-props-to-children
# Or
npm install --save map-props-to-children

Usage

Importing

# ES6+
import mapPropsToChildren from "map-props-to-children";

# ES5
var mapPropsToChildren = require("map-props-to-children");

API

This library includes the following function:

mapPropsToChildren({ children, injectedProps, props })

Returns a list of the children components provided, but with new props injected into each child's props (shallow). The exported function has a fixed arity of 1 that accepts the following properties on the argument object:

  • children: List of child components to clone and inject props into. Its existing props takes highest precedence
  • injectedProps: The props that will be injected into child components. It takes second-highest precedence
  • props: Props from the container component calling this function. Allows for the container to define some default prop values. It takes the lowest precedence

Use Case

Suppose your project renders this dumb component:

export default function StockTicker({ stocks }) {
  return (
    <div>
      <h2>Stock Ticker</h2>
      {stocks.map((stock, index) => (
        <p key={index}><b>{stock.name}:</b> {stock.price}</p>
      )}
    </div>
  );
}

You will pass props to StockTicker via a Redux container, but you don't want the container know anything about the component so that they are decoupled from each other. A theoretical app that uses this container would look like so:

export default function App() {
  return (
    <StockItemListContainer>
      <StockTicker />
    </StockItemListContainer>
  );
}

The StockItemListContainer knows nothing about the types of its children components, but can still inject props from the Redux state into them using mapPropsToChildren:

function StockItemListContainer({ items, children, ...props }) {
  const injectedProps = { items };
  return (
    <div>
      {mapPropsToChildren({ children, injectedProps, props })}
    </div>
  );
}

function mapStateToProps(state) {
  return {
    items: state.items,
  };
}

export default connect(mapStateToProps)(StockItemListContainer);

Now, you can easily reuse the same container for a different component that accepts the items prop:

export default function Widget() {
  return (
    <StockItemListContainer>
      <StockWidget />
    </StockItemListContainer>
  );
}

License

This project is licensed under the MIT License. See LICENSE for more details.

Acknowledgements

This project was made possible through contributions from the following people:

map-props-to-children's People

Contributors

ecbrodie avatar jakxz avatar

Stargazers

Ned Schwartz avatar  avatar

Watchers

James Herdman avatar Ian Marcinkowski avatar seano avatar Adam Kerr avatar Jason Yuen avatar Joe Auty avatar Clemens Park avatar Steph avatar Chris Gow avatar Arturo Pie avatar James Cloos avatar Paul Sobocinski avatar Shahriyar Nasir avatar Nerman Nicholas avatar Dan Ambrogio avatar  avatar  avatar  avatar Ian Penney avatar Melissa Mohan avatar  avatar Adam Lomas avatar Seema H avatar Haider Alshamma avatar  avatar  avatar Jeff avatar Jordan avatar Navila avatar  avatar  avatar

map-props-to-children's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.