npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

map-props-to-children

v1.0.0

Published

Lightweight function to inject props into child React components

Downloads

244

Readme

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. Accepts the following input:

  • 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 => (
        <p><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: