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 🙏

© 2026 – Pkg Stats / Ryan Hefner

map-props-changes-to-callbacks

v1.1.0

Published

React's HOC utility to easily map props changes in a component to the specified callbacks.

Readme

map-props-changes-to-callbacks

React's HOC utility to easily map props changes in a component to the specified callbacks.

Why?

There are some cases when we need to do something when some particular prop change occurs in our component , eg. isLoading prop changes its value from false to true and vice versa. Wouldn't it be great if we could easily map these transitions to well-named callbacks like this:

// isLoading: false => true
onLoadingStart() {
  // do something when loading starts
}

// isLoading: true => false
onLoadingEnd() {
  // do something when loading ends
}

Well, map-props-changes-to-callbacks does exactly that.

Installation

npm i map-props-changes-to-callbacks -S

Import package

With ES2015:

import { withCallbacks, withOrderedCallbacks } from 'map-props-changes-to-callbacks'

With ES5:

var withCallbacks = require('map-props-changes-to-callbacks').withCallbacks
var withOrderedCallbacks = require('map-props-changes-to-callbacks').withOrderedCallbacks

Usage

withCallbacks

withCallbacks HOC is useful when you don't care about the callbacks execution order. Also it will fire every callback that matched provided rule on every props change.

import React, { Component } from 'react'  
import { connect } from 'react-redux'  
import { compose } from 'redux'  
import { withCallbacks } from 'map-props-changes-to-callbacks'

class MyComponent extends Component {  
  onUploadStart() {
    console.log('onUploadStart')
  }

  onUploadEnd() {
    console.log('onUploadEnd')
  }

  onUploadError() {
    console.log('onUploadError')
  }
}

/**
  Mappings is an object where
  keys are callbacks names and values are
  functions accepting previous and next props
  and returning true or false to tell the HOC 
  to call callback method.
**/
const mappings = {  
  onUploadStart: (prev, next) => !prev.isUploading && next.isUploading,
  onUploadEnd: (prev, next) => prev.isUploading && !next.isUploading,
  onUploadError: (prev, next) => !prev.error && next.error,
}
const enhance = compose(  
  connect( ({ documents }) => ({isUploading: documents.isUploading, error: documents.error}) ),
  withCallbacks(mappings)
)

export default enhance(MyComponent)  

withOrderedCallbacks

withOrderedCallbacks is useful when you want your callbacks to be called in specified order. Also, you can suppress further callbacks execution on specific props change if you provide suppressAfter option. Mappings, unlike as in withCallbacks HOC, are provided as an array of objects. Remember that the order of objects in the array defines an order of execution!

import React, { Component } from 'react'  
import { connect } from 'react-redux'  
import { compose } from 'redux'  
import { withOrderedCallbacks } from 'map-props-changes-to-callbacks'

class MyComponent extends Component {  
  onUploadStart() {
    console.log('onUploadStart')
  }

  onUploadEnd() {
    console.log('onUploadEnd')
  }

  onUploadError() {
    console.log('onUploadError')
  }
}

/**
  Mappings is an array of objects with following properties:
  
  name (String)           -> name of the callback
  rule (Function)         -> predicate describing when to call this function
  suppressAfter (boolean) -> if set to true will suppress next callbacks
                            specified in the mappings array from execution (OPTIONAL)
  
**/


/**
  In this case every time when the onUploadEnd rule is be true
  onUploadError (and any other callback specified AFTER the onUploadEnd) 
  will not be called, because suppressAfter option is set to true for onUploadEnd
**/
const mappings = [
  {
    name: 'onUploadStart',
    rule: (prev, next) => !prev.isUploading && next.isUploading,
  },
  {
    name: 'onUploadEnd',
    rule: (prev, next) => prev.isUploading && !next.isUploading,
    suppressAfter: true,
  },
  {
    name: 'onUploadError',
    rule: (prev, next) => !prev.error && next.error,
  }
]

const enhance = compose(  
  connect( ({ documents }) => ({isUploading: documents.isUploading, error: documents.error}) ),
  withOrderedCallbacks(mappings)
)

export default enhance(MyComponent)  

IMPORTANT NOTE

If you're composing multiple HOCs with compose utility from the redux library make sure that map-props-changes-to-callbacks comes as the outermost in the chain. Otherwise callback mappings won't work!