1.0.2 • Published 6 years ago

what-props-changed v1.0.2

Weekly downloads
4
License
MIT
Repository
-
Last release
6 years ago

what-props-changed

A simple React development tool to help find the props that caused a Component to update.

By default, it will log to the browser console any detected changes, with their original and new values.

// Example Output:

Sidebar, props changed: 1
 {
  "sidebar": {
    "old": "menu",
    "new": "list"
  }
}

Installation

# yarn
yarn add what-props-changed

# npm
npm install --save what-props-changed

Usage

import * as React from 'react'
import whatPropsChanged from 'what-props-changed'

class Sidebar extends React.Component {

  componentWillReceiveProps (nextProps) {
    whatPropsChanged(this.props, nextProps, 'Sidebar')
  }

  render() {
    return <div>{this.props.title}</div>
  }
}

Arguments

OptionsTypeDefaultDescription
propsobject{}Original (props or source) object
nextPropsobject{}Updated (props or source) object
namestring''Optional name or helpful description that will be logged with output
optionsobjectoptionsOptional options object, see options below.

Options

An object you can pass as the 4th argument when calling whatPropsChanged

OptionsTypeDefaultDescription
doReturnbooleanfalseReturn the changes object
stringbooleantrueLogs the changes as JSON.stringified for easier console viewing
logbooleantrueLog prop changes object to console

Notes

what-props-changed provides a default export, you can use whatPropsChanged or whatChanged etc. You can also pass it generic objects to compare instead of props. It will iterate over the first object argument.

Additional Documentation

See src/index.js for source code.

/**
 * Logs to console, props that caused a Component to update
 *
 * @param  {Object} props     Component current props
 * @param  {Object} nextProps Component updated props
 * @param  {String} name      Component name or description for log
 * @param  {Object} opts      Additional options
 * @return {undefined}        Logs changes to console
 * @example

  // Usage

  componentWillReceiveProps (nextProps) {
    whatPropsChanged(this.props, nextProps, 'Sidebar')
  }

  // Add options if desired, as a 4th argument.
  // Default values included below

  const options = {
    doReturn: false, // return changed props object
    string: true, // log object instead of a JSON.stringified object
    log: true, // log results to console
  }

 */