{"name":"react-side-effect","version":"1.1.0","description":"Create components whose prop changes map to a global side effect","main":"lib/index.js","scripts":{"build":"babel src --out-dir lib","clean":"rimraf lib","prepublish":"npm test && npm run clean && npm run build","test":"mocha","test:watch":"mocha --watch","test:cov":"babel-node ./node_modules/.bin/isparta cover ./node_modules/.bin/_mocha"},"repository":{"type":"git","url":"git+https://github.com/gaearon/react-side-effect.git"},"keywords":["react","component","side","effect"],"author":{"name":"Dan Abramov","email":"dan.abramov@me.com","url":"http://github.com/gaearon"},"license":"MIT","bugs":{"url":"https://github.com/gaearon/react-side-effect/issues"},"homepage":"https://github.com/gaearon/react-side-effect","dependencies":{"exenv":"^1.2.1","shallowequal":"^0.2.2"},"devDependencies":{"babel":"^5.8.23","chai":"^3.2.0","isparta":"^3.0.4","jsdom":"^6.3.0","mocha":"^2.3.0","react":"^0.13.3","rimraf":"^2.4.3"},"files":["LICENSE","README.md","lib/"],"gitHead":"a540a19512bdd62bdeab8a82e129ebef52b33513","_id":"react-side-effect@1.1.0","_shasum":"57209f7ebc940d55e0fda82fe51422654175d609","_from":".","_npmVersion":"3.8.6","_nodeVersion":"5.12.0","_npmUser":{"name":"lourd","email":"louis@grovelabs.io"},"dist":{"shasum":"57209f7ebc940d55e0fda82fe51422654175d609","tarball":"http://registry-npm.ywwl.com/react-side-effect/download/react-side-effect-1.1.0.tgz","integrity":"sha512-SREmvulNWli6cBoKbw63SB4JNwOL8LGxGQGLp3S9gBgWmTO5kfMwEyKdUReC8lLt8ooUTtTWvQMBJ5e0Ph6mWw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCzUybBNQaA6TEnR5zwNPhN6pmq1fnwM4xGio45IUgrKAIgeor4Ylaa8JJK/ugm0wckvfC02b5OA7f9GphvdtsCBRM="}],"size":4257,"noattachment":false,"key":"/react-side-effect/-/react-side-effect-1.1.0.tgz"},"maintainers":[{"name":"gaearon","email":"dan.abramov@gmail.com"},{"name":"lourd","email":"louis@grovelabs.io"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/react-side-effect-1.1.0.tgz_1483903511938_0.8280009301379323"},"directories":{},"readme":"# React Side Effect [![Downloads](https://img.shields.io/npm/dm/react-side-effect.svg)](https://npmjs.com/react-side-effect) [![npm version](https://img.shields.io/npm/v/react-side-effect.svg?style=flat)](https://www.npmjs.com/package/react-side-effect)\n\nCreate components whose prop changes map to a global side effect.\n\n## Installation\n\n```\nnpm install --save react-side-effect\n```\n\n### As a script tag\n\n#### Development\n\n```html\n<script src=\"https://unpkg.com/react/umd/react.development.js\" type=\"text/javascript\"></script>\n<script src=\"https://unpkg.com/react-side-effect/lib/index.umd.js\" type=\"text/javascript\"></script>\n```\n\n#### Production\n\n```html\n<script src=\"https://unpkg.com/react/umd/react.production.min.js\" type=\"text/javascript\"></script>\n<script src=\"https://unpkg.com/react-side-effect/lib/index.umd.min.js\" type=\"text/javascript\"></script>\n```\n\n## Use Cases\n\n* Setting `document.body.style.margin` or background color depending on current screen;\n* Firing Flux actions using declarative API depending on current screen;\n* Some crazy stuff I haven't thought about.\n\n## How's That Different from `componentDidUpdate`?\n\nIt gathers current props across *the whole tree* before passing them to side effect. For example, this allows you to create `<BodyStyle style>` component like this:\n\n```jsx\n// RootComponent.js\nreturn (\n  <BodyStyle style={{ backgroundColor: 'red' }}>\n    {this.state.something ? <SomeComponent /> : <OtherComponent />}\n  </BodyStyle>\n);\n\n// SomeComponent.js\nreturn (\n  <BodyStyle style={{ backgroundColor: this.state.color }}>\n    <div>Choose color: <input valueLink={this.linkState('color')} /></div>\n  </BodyStyle>\n);\n```\n\nand let the effect handler merge `style` from different level of nesting with innermost winning:\n\n```js\nimport { Component, Children } from 'react';\nimport PropTypes from 'prop-types';\nimport withSideEffect from 'react-side-effect';\n\nclass BodyStyle extends Component {\n  render() {\n    return Children.only(this.props.children);\n  }\n}\n\nBodyStyle.propTypes = {\n  style: PropTypes.object.isRequired\n};\n\nfunction reducePropsToState(propsList) {\n  var style = {};\n  propsList.forEach(function (props) {\n    Object.assign(style, props.style);\n  });\n  return style;\n}\n\nfunction handleStateChangeOnClient(style) {\n  Object.assign(document.body.style, style);\n}\n\nexport default withSideEffect(\n  reducePropsToState,\n  handleStateChangeOnClient\n)(BodyStyle);\n```\n\nOn the server, you’ll be able to call `BodyStyle.peek()` to get the current state, and `BodyStyle.rewind()` to reset for each next request. The `handleStateChangeOnClient` will only be called on the client.\n\n## API\n\n#### `withSideEffect: (reducePropsToState, handleStateChangeOnClient, [mapStateOnServer]) -> ReactComponent -> ReactComponent`\n\nA [higher-order component](https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750) that, when mounting, unmounting or receiving new props, calls `reducePropsToState` with `props` of **each mounted instance**. It is up to you to return some state aggregated from these props.\n\nOn the client, every time the returned component is (un)mounted or its props change, `reducePropsToState` will be called, and the recalculated state will be passed to `handleStateChangeOnClient` where you may use it to trigger a side effect.\n\nOn the server, `handleStateChangeOnClient` will not be called. You will still be able to call the static `rewind()` method on the returned component class to retrieve the current state after a `renderToString()` call. If you forget to call `rewind()` right after `renderToString()`, the internal instance stack will keep growing, resulting in a memory leak and incorrect information. You must call `rewind()` after every `renderToString()` call on the server.\n\nFor testing, you may use a static `peek()` method available on the returned component. It lets you get the current state without resetting the mounted instance stack. Don’t use it for anything other than testing.\n\n## Usage\n\nHere's how to implement [React Document Title](https://github.com/gaearon/react-document-title) (both client and server side) using React Side Effect:\n\n```js\nimport React, { Children, Component } from 'react';\nimport PropTypes from 'prop-types';\nimport withSideEffect from 'react-side-effect';\n\nclass DocumentTitle extends Component {\n  render() {\n    if (this.props.children) {\n      return Children.only(this.props.children);\n    } else {\n      return null;\n    }\n  }\n}\n\nDocumentTitle.propTypes = {\n  title: PropTypes.string.isRequired\n};\n\nfunction reducePropsToState(propsList) {\n  var innermostProps = propsList[propsList.length - 1];\n  if (innermostProps) {\n    return innermostProps.title;\n  }\n}\n\nfunction handleStateChangeOnClient(title) {\n  document.title = title || '';\n}\n\nexport default withSideEffect(\n  reducePropsToState,\n  handleStateChangeOnClient\n)(DocumentTitle);\n```\n","publish_time":1483903512611,"_cnpm_publish_time":1483903512611,"dist-tags":{"latest":"2.1.2"}}