{"name":"react-side-effect","version":"2.0.0","description":"Create components whose prop changes map to a global side effect","main":"lib/index.js","scripts":{"build":"node scripts/build.js","clean":"rimraf lib","prepare":"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","contributors":[{"name":"Louis DeScioli","url":"https://descioli.design"}],"peerDependencies":{"react":"^16.9.0"},"dependencies":{"shallowequal":"^1.0.1"},"devDependencies":{"@babel/cli":"7.0.0","@babel/core":"7.0.0","@babel/node":"7.0.0","@babel/plugin-proposal-class-properties":"7.0.0","@babel/plugin-proposal-object-rest-spread":"7.0.0","@babel/preset-env":"7.0.0","@babel/preset-react":"7.0.0","@babel/register":"7.0.0","babel-plugin-add-module-exports":"^0.2.1","chai":"^3.2.0","create-react-class":"^15.6.3","enzyme":"^3.10.0","enzyme-adapter-react-16":"^1.14.0","gzip-size":"^4.1.0","isparta":"^4.0.0","jsdom":"^9.9.1","mocha":"^3.2.0","pretty-bytes":"^4.0.2","react":"^16.9.0","react-dom":"^16.9.0","rimraf":"^2.4.3","rollup":"^1.20.3","rollup-plugin-babel":"^4.0.0","rollup-plugin-commonjs":"^8.3.0","rollup-plugin-node-resolve":"^3.0.3","rollup-plugin-uglify":"^3.0.0"},"gitHead":"7c9f83e6fdf9458488c2e840020b9358250ede31","_id":"react-side-effect@2.0.0","_nodeVersion":"10.16.3","_npmVersion":"6.9.0","dist":{"integrity":"sha512-ttqKfExJRNQ2Kypffonu8Ss4ZPCanXMA9+KiHjFKs7sX3uIdp2Fev7lWXd54RoevndLiAo4/jsuMla1/wf4DEA==","shasum":"47163e0ede952929347e0894e02239f44f3f0e63","tarball":"http://registry-npm.ywwl.com/react-side-effect/download/react-side-effect-2.0.0.tgz","fileCount":7,"unpackedSize":22757,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdaqXFCRA9TVsSAnZWagAAHvIP/i6m3ml5ULD3dO3RHTCx\npi4EQipLn19eAWwlFCYVqEJvLZ51nP+v/GBlWXS80FBbZCM1nlD6Uc1c7J9V\nSvziXFSL5SPjQ9VkGVQNl86eUb9ZmNuOsok/OWeYIbcPDT/0fHL/8GDhne2f\nQT6iWBBX5og8Ss1CZjk+SIu3dnDHgb2oNtNuNcvTXn9kvXCOyrW1L6y0B+nm\n/38LQ8Hvu9fFux5gWUV4uwOb6/2gozyQJ0xORPPJYI1A/1thQ+rVwdM6WzWM\nhcx5XZxSjNSAwfIzeWUOEBPFr23Mv4CK2TI1XF3bbiJUvorbX/Uq210QZ34Z\nlam8kd5Bs008+PB2p+efcT3EzGProYSTwqFf4f6E9muJdXUZK5Y1iBtCn7QR\nGlmF5RAHESq81wMbh6es0m5yr3tCVC1I7AbNf/EXa2qeVjKT5RHg1DlbLWLb\nQvoGRLc2bDj4ZQMaBRjOPkajXySQIMkaW6RbrjfkcKYWZ9eGt14e2Uj1tnrp\nJAqFo2zGNn9bQ39FyjgpzDKEv30xN3AXV+EK+DrqqX/WIbWonVcZnoVNzVlx\nutyNoYYcRzXRdgIcjA3EiVYBzNSPnai+xhsf4Y7geQ9mu7tktBmCa/0Tuo9M\nCVXy9M9C3IVMMCpaNhr4qjBghPaF2apM2YnqyQCqNJC9qsky+wzwVY4JmiIN\nlaW/\r\n=DpMS\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEMCIDj/C7uvS05HOeEwuSPahVIi9A9gVIqKdukxSjkcxuB/Ah9QqNRgUiIGhFW/YitU07XfTy5f+3WreLLpMDQrTqUs"}],"size":5974,"noattachment":false,"key":"/react-side-effect/-/react-side-effect-2.0.0.tgz"},"maintainers":[{"name":"gaearon","email":"dan.abramov@gmail.com"},{"name":"lourd","email":"louis@grovelabs.io"}],"_npmUser":{"name":"lourd","email":"louis.descioli@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/react-side-effect_2.0.0_1567270340859_0.827852896274837"},"_hasShrinkwrap":false,"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":1567270340988,"_cnpm_publish_time":1567270340988,"dist-tags":{"latest":"2.1.2"}}