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

map-props

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

map-props

A higher order component to map React props

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
890
increased by0.56%
Maintainers
1
Weekly downloads
 
Created
Source

#map-props

NPM Version NPM Downloads Build Status devDependency Status

map-props wraps a React component and allows mapping prop values passed in to other prop values expected by the wrapped component.


Installation

npm install --save map-props

Release Notes

This project follows SemVer and each release is posted on the Release Notes page.

Example

import React, {Component, PropTypes} from 'react';
import mapProps from 'map-props';

class UserProfile extends Component {
  static propTypes = {
    firstName: PropTypes.string.isRequired,
    lastName: PropTypes.string.isRequired
  }
  
  render() {
    const {firstName, lastName} = this.props;
    return <div className="user-profile">{firstName} {lastName}</div>;
  }
}

// wrap this component to create a component that expects only a fullName prop
UserProfile = mapProps({
  firstName: props => props.fullName.split(' ')[0],
  lastName: props => props.fullName.split(' ')[1]
})(UserProfile);

export default UserProfile;

Then render this component:

<UserProfile fullName="Bobby Tables"/>

Benefits

By mapping props using a Higher Order Component, you can increase the reusability of other "dumb" (pure and stateless) components.

Complimentary

This library was inspired by, and intended to be used in conjunction with redux, reselect and redux-form, although it does not depend on any of those libraries and can be used in any React-based project.

ES7 Decorator Sugar

Using ES7 decorator proposal, the example above could be written as:

@mapProps({
  firstName: props => props.fullName.split(' ')[0],
  lastName: props => props.fullName.split(' ')[1]
})
export default class UserProfile extends Component {

You can enable ES7 decorators with Babel Stage 1. Note that decorators are experimental, and this syntax might change or be removed later.

API

mapProps(mapping:Object)

-mapping : Object

a mapping from propName to a function that takes all the props and produces the value to pass to the wrapped component as propName.

This is an extremely young library, so the API may change. Comments and feedback welcome.

Keywords

FAQs

Package last updated on 31 Aug 2015

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