Create components whose nested prop changes map to a global side effect
Installations
npm install react-side-effect
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
16.14.0
NPM Version
8.3.1
Score
98.4
Supply Chain
99
Quality
78.2
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
gaearon
Download Statistics
Total Downloads
504,492,241
Last Day
381,512
Last Week
2,035,603
Last Month
8,540,722
Last Year
96,190,330
GitHub Statistics
MIT License
1,222 Stars
112 Commits
77 Forks
15 Watchers
9 Branches
16 Contributors
Updated on Feb 11, 2025
Bundle Size
1.72 kB
Minified
829.00 B
Minified + Gzipped
Package Meta Information
Latest Version
2.1.2
Package Id
react-side-effect@2.1.2
Unpacked Size
20.13 kB
Size
5.29 kB
File Count
7
NPM Version
8.3.1
Node Version
16.14.0
Total Downloads
Cumulative downloads
Total Downloads
504,492,241
Last Day
-0.7%
381,512
Compared to previous day
Last Week
4.2%
2,035,603
Compared to previous week
Last Month
47.2%
8,540,722
Compared to previous month
Last Year
0.4%
96,190,330
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
23
React Side Effect

Create components whose prop changes map to a global side effect.
Installation
npm install --save react-side-effect
As a script tag
Development
1<script src="https://unpkg.com/react/umd/react.development.js" type="text/javascript"></script> 2<script src="https://unpkg.com/react-side-effect/lib/index.umd.js" type="text/javascript"></script>
Production
1<script src="https://unpkg.com/react/umd/react.production.min.js" type="text/javascript"></script> 2<script src="https://unpkg.com/react-side-effect/lib/index.umd.min.js" type="text/javascript"></script>
Use Cases
- Setting
document.body.style.margin
or background color depending on current screen; - Firing Flux actions using declarative API depending on current screen;
- Some crazy stuff I haven't thought about.
How's That Different from componentDidUpdate
?
It 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:
1// RootComponent.js 2return ( 3 <BodyStyle style={{ backgroundColor: 'red' }}> 4 {this.state.something ? <SomeComponent /> : <OtherComponent />} 5 </BodyStyle> 6); 7 8// SomeComponent.js 9return ( 10 <BodyStyle style={{ backgroundColor: this.state.color }}> 11 <div>Choose color: <input valueLink={this.linkState('color')} /></div> 12 </BodyStyle> 13);
and let the effect handler merge style
from different level of nesting with innermost winning:
1import { Component, Children } from 'react'; 2import PropTypes from 'prop-types'; 3import withSideEffect from 'react-side-effect'; 4 5class BodyStyle extends Component { 6 render() { 7 return Children.only(this.props.children); 8 } 9} 10 11BodyStyle.propTypes = { 12 style: PropTypes.object.isRequired 13}; 14 15function reducePropsToState(propsList) { 16 var style = {}; 17 propsList.forEach(function (props) { 18 Object.assign(style, props.style); 19 }); 20 return style; 21} 22 23function handleStateChangeOnClient(style) { 24 Object.assign(document.body.style, style); 25} 26 27export default withSideEffect( 28 reducePropsToState, 29 handleStateChangeOnClient 30)(BodyStyle);
On 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.
API
withSideEffect: (reducePropsToState, handleStateChangeOnClient, [mapStateOnServer]) -> ReactComponent -> ReactComponent
A higher-order component 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.
On 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.
On 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.
For 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.
Usage
Here's how to implement React Document Title (both client and server side) using React Side Effect:
1import React, { Children, Component } from 'react'; 2import PropTypes from 'prop-types'; 3import withSideEffect from 'react-side-effect'; 4 5class DocumentTitle extends Component { 6 render() { 7 if (this.props.children) { 8 return Children.only(this.props.children); 9 } else { 10 return null; 11 } 12 } 13} 14 15DocumentTitle.propTypes = { 16 title: PropTypes.string.isRequired 17}; 18 19function reducePropsToState(propsList) { 20 var innermostProps = propsList[propsList.length - 1]; 21 if (innermostProps) { 22 return innermostProps.title; 23 } 24} 25 26function handleStateChangeOnClient(title) { 27 document.title = title || ''; 28} 29 30export default withSideEffect( 31 reducePropsToState, 32 handleStateChangeOnClient 33)(DocumentTitle);

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 7/12 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 25 are checked with a SAST tool
Reason
45 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-qh2h-chj9-jffq
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-cf4h-3jhx-xvhq
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-662x-fhqg-9p8v
- Warn: Project is vulnerable to: GHSA-394c-5j6w-4xmx
- Warn: Project is vulnerable to: GHSA-78cj-fxph-m83p
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
Score
2.4
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-side-effect
react-clientside-effect
Create components whose prop changes map to a global side effect
@types/react-side-effect
TypeScript definitions for react-side-effect
use-isomorphic-layout-effect
A React helper hook for scheduling a layout effect with a fallback to a regular effect for environments where layout effects should not be used (such as server-side rendering).
@airma/react-effect
This is a react async state management tool