{"name":"react-side-effect","version":"2.1.2","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 || ^17.0.0 || ^18.0.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":"e17202b58de7921390bfeb71c36d1e0b31e84535","_id":"react-side-effect@2.1.2","_nodeVersion":"16.14.0","_npmVersion":"8.3.1","dist":{"integrity":"sha512-PVjOcvVOyIILrYoyGEpDN3vmYNLdy1CajSFNt4TDsVQC5KpTijDvWVoR+/7Rz2xT978D8/ZtFceXxzsPwZEDvw==","shasum":"dc6345b9e8f9906dc2eeb68700b615e0b4fe752a","tarball":"http://registry-npm.ywwl.com/react-side-effect/download/react-side-effect-2.1.2.tgz","fileCount":7,"unpackedSize":20608,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICtvUeg/CoPid+w2K/BGFLcGrv8nD6bz4C+LwUEou32nAiALm9MPCoEkGDt5eXaztO+HV1h/DNuod+/jAzRt6WQzNw=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiuM7QACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpWbg//QD96PL3+cXa3L+w6E7/YLgaVsDHk6aemHJxtfwLY+N8cQNVC\r\nGbu2D5uSrZhRrTiZIsgs7CXz2Zekvsfwc1YSxLNZOxw7PC/c/4E0M5q2rnhX\r\nRs/1u8ZPH+jcWxBHqmXf26coEjCVNc/d63iDx7yzTplxal9+hMD/mxu9jzG4\r\nwgosG1wirm30Xc3Vv+NVGhEYfTXsBqEr3FBjviK3HxcW8wZ4ewWA1GcuUopf\r\nScW/7+OBQoz/NqHmPXSikDnbrQmAJd8t1PLXJl1ZDHsoQot9m1AYBoPJyzGP\r\niJrxqhQ/U9wnn+DhvFYDk55J9YiIdBWZ0sw8QtY2wb/baT/PRx7ek72h0awD\r\n0Tnllh6BjPZEbEp5BziEw+NGQkcfSmlPKuf2cPM6UlOO81e2+TainKpUa1AZ\r\nS014PnsL8YsCWftUZBPk0myKrNKWEam/XQ8yGUVf8C0LRrF1DhFhujU1d7/v\r\n3I586KM+YyAUVd/9V58/xsymVhnV9aaqLYcrI48NM7tV3ci/5+I3gxlfMMIS\r\ncekvjdandeldm88J3uvmBzP26VtThY/QqRSsDn5AP8MfTn5IJllDD+eFwYCF\r\nHmV9H4uC6XFLulhUwd4bbHWE19jvL1JqQeG0sBHqwj8tymwpxBRrFn0Yf8RG\r\nUxPG1ga1sEH+aJRg0x/7GT/+QVow6NE3TUU=\r\n=OOMr\r\n-----END PGP SIGNATURE-----\r\n","size":5422,"noattachment":false,"key":"/react-side-effect/-/react-side-effect-2.1.2.tgz"},"_npmUser":{"name":"lourd","email":"louis.descioli@gmail.com"},"directories":{},"maintainers":[{"name":"gaearon","email":"dan.abramov@gmail.com"},{"name":"lourd","email":"louis.descioli@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/react-side-effect_2.1.2_1656278736233_0.6250751323880388"},"_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":1656278736409,"_cnpm_publish_time":1656278736409,"dist-tags":{"latest":"2.1.2"}}