{"name":"react-clientside-effect","version":"1.2.7","description":"Create components whose prop changes map to a global side effect","main":"lib/index.js","module":"lib/index.es.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/thekashey/react-clientside-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/thekashey/react-clientside-effect/issues"},"homepage":"https://github.com/thekashey/react-clientside-effect","contributors":[{"name":"Louis DeScioli","email":"louis.descioli@gmail.com"}],"peerDependencies":{"react":"^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^19.0.0-rc"},"dependencies":{"@babel/runtime":"^7.12.13"},"devDependencies":{"@babel/cli":"^7.12.13","@babel/core":"^7.12.13","@babel/node":"^7.12.13","@babel/plugin-proposal-class-properties":"^7.12.13","@babel/plugin-proposal-object-rest-spread":"^7.12.13","@babel/plugin-transform-runtime":"^7.12.13","@babel/preset-env":"^7.12.13","@babel/preset-react":"^7.12.13","@babel/register":"^7.12.13","babel-plugin-add-module-exports":"^0.2.1","chai":"^3.2.0","enzyme":"^2.7.0","exenv":"^1.2.2","gzip-size":"^4.1.0","isparta":"^4.0.0","jsdom":"^11.12.0","jsdom-global":"^3.0.2","mocha":"^3.2.0","pretty-bytes":"^4.0.2","react":"^15.4.2","react-addons-test-utils":"^15.4.2","react-dom":"^15.4.2","rimraf":"^2.4.3","rollup":"^0.67.1","rollup-plugin-babel":"^4.0.3","rollup-plugin-node-resolve":"^3.0.3","rollup-plugin-uglify":"^3.0.0"},"gitHead":"ed713458e42e7810a1cec972e9e6ab772a3182e1","_id":"react-clientside-effect@1.2.7","_nodeVersion":"16.20.2","_npmVersion":"8.19.4","dist":{"integrity":"sha512-gce9m0Pk/xYYMEojRI9bgvqQAkl6hm7ozQvqWPyQx+kULiatdHgkNM1QG4DQRx5N9BAzWSCJmt9mMV8/KsdgVg==","shasum":"78eb62e3be36208d4d8d5b2668ae630a32deca73","tarball":"http://registry-npm.ywwl.com/react-clientside-effect/download/react-clientside-effect-1.2.7.tgz","fileCount":7,"unpackedSize":16672,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHcyZKmjpnXeDFAZ/GdbW+AogzpqQWt4abUn18f9D2X3AiBhiKEg7htOYkMdMnXQtdRPZTW0OkGjF63IdzdpGBGAMQ=="}],"size":4841,"noattachment":false,"key":"/react-clientside-effect/-/react-clientside-effect-1.2.7.tgz"},"_npmUser":{"name":"kashey","email":"thekashey@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/react-clientside-effect_1.2.7_1734229928843_0.6724585855543594"},"_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\nThis is client-side variation of the original react-side-effect, for client-side\ncomponents.\nIt does nothing on server side.\n\n## Installation\n\n```\nnpm install --save react-clientside-effect\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```js\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\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\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\n","publish_time":1734229929027,"maintainers":[{"name":"kashey","email":"thekashey@gmail.com"}],"_cnpm_publish_time":1734229929027,"dist-tags":{"latest":"1.2.8"}}