{"name":"react-side-effect","version":"2.1.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.3.0"},"dependencies":{},"devDependencies":{"@babel/cli":"^7.5.5","@babel/core":"^7.5.5","@babel/node":"^7.5.5","@babel/plugin-proposal-class-properties":"^7.5.5","@babel/preset-env":"^7.5.5","@babel/preset-react":"^7.0.0","@babel/register":"^7.5.5","babel-plugin-add-module-exports":"^1.0.2","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-uglify":"^3.0.0"},"gitHead":"21e922877c3266410d8acda3cc16b28d2492e6da","_id":"react-side-effect@2.1.0","_nodeVersion":"10.16.3","_npmVersion":"6.9.0","dist":{"integrity":"sha512-IgmcegOSi5SNX+2Snh1vqmF0Vg/CbkycU9XZbOHJlZ6kMzTmi3yc254oB1WCkgA7OQtIAoLmcSFuHTc/tlcqXg==","shasum":"1ce4a8b4445168c487ed24dab886421f74d380d3","tarball":"http://registry-npm.ywwl.com/react-side-effect/download/react-side-effect-2.1.0.tgz","fileCount":7,"unpackedSize":20586,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdcBsWCRA9TVsSAnZWagAAjYMQAJgbxk5oFvjVGqZl3hd6\nsLKmzfRMAQi/54/FFyLAl6AVgRANtLVC0ZCfQ2Vs+fbqu4fDwYE1ywLipuK6\naY+YIHvaUExPBab/RSFY6j9wP3+yrwGkqWOVzSPMAvHDg8SsRZmKdSFH2gqI\nry8HBQu7IfdefWwyHjGKAcHHlKYBtHjkeAyYOEO6GdAA+P0lOa2c/lObeE86\nlOtFS9Go3S+Zv5LCJo/R3ZCKKs3TQ5jrq/wPvDIF6F7VRtl4aRc/t8xH25bh\n7u1pe6JS2Jmxs6q6lAiYfqHnGuUJ3iHzzRFtZMmLlddptWnoVbTwGDBqTvUf\n68GXCQF+ijwWZ1aWlTeZGwNZX5edt/yvsrO01eK9VnzzvLPvYLv39yLeqvzA\n2B8KbkaqivshhX6ZPXwbyoaEmf4W4EjrSgz6QJGPb2vAOJu4pjDjugpHbGEZ\nFFu2Eoz25XfpfvldpcvFxI3M0l7t8biqbZE0IVThrCfZ4s52uLE2/2Z3cnq5\nBySOy9iKIdL4VMlJR3kjvnVcUY8pnZ2Y/HYgXjHFV8LDqw04zPFntM4AbWQO\n5QSx21t72aMUEAuO2t4oJ0s3VfwXk3EvDZA1I1kuzIStroHAHvVyEl77UzUr\nl2S4h2JPuEil3LRcbCvo3p866lHH7mwsc3alNx/oY3irKqXRCWu8Y5xSi8kW\nWnhO\r\n=ETJN\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAq5CI+PlGxngj72ZIlrJY1/lfbLq+X/aLtB1t1mB2eIAiEAwwzVCp3k1Q9lBSdkcG+6WOP6YgkNtbbffoOqMsKE+Nk="}],"size":5385,"noattachment":false,"key":"/react-side-effect/-/react-side-effect-2.1.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.1.0_1567628053685_0.7843533141138546"},"_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":1567628053797,"_cnpm_publish_time":1567628053797,"dist-tags":{"latest":"2.1.2"}}