
Security News
Security Community Slams MIT-linked Report Claiming AI Powers 80% of Ransomware
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.
react-clientside-effect
Advanced tools
Create components whose prop changes map to a global side effect
Create components whose prop changes map to a global side effect.
This is client-side variation of the original react-side-effect, for client-side components. It does nothing on server side.
npm install --save react-clientside-effect
document.body.style.margin or background color depending on current screen;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:
// RootComponent.js
return (
  <BodyStyle style={{ backgroundColor: 'red' }}>
    {this.state.something ? <SomeComponent /> : <OtherComponent />}
  </BodyStyle>
);
// SomeComponent.js
return (
  <BodyStyle style={{ backgroundColor: this.state.color }}>
    <div>Choose color: <input valueLink={this.linkState('color')} /></div>
  </BodyStyle>
);
and let the effect handler merge style from different level of nesting with innermost winning:
import { Component, Children } from 'react';
import PropTypes from 'prop-types';
import withSideEffect from 'react-side-effect';
class BodyStyle extends Component {
  render() {
    return Children.only(this.props.children);
  }
}
BodyStyle.propTypes = {
  style: PropTypes.object.isRequired
};
function reducePropsToState(propsList) {
  var style = {};
  propsList.forEach(function (props) {
    Object.assign(style, props.style);
  });
  return style;
}
function handleStateChangeOnClient(style) {
  Object.assign(document.body.style, style);
}
export default withSideEffect(
  reducePropsToState,
  handleStateChangeOnClient
)(BodyStyle);
withSideEffect: (reducePropsToState, handleStateChangeOnClient, [mapStateOnServer]) -> ReactComponent -> ReactComponentA 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.
Here's how to implement React Document Title (both client and server side) using React Side Effect:
import React, { Children, Component } from 'react';
import PropTypes from 'prop-types';
import withSideEffect from 'react-side-effect';
class DocumentTitle extends Component {
  render() {
    if (this.props.children) {
      return Children.only(this.props.children);
    } else {
      return null;
    }
  }
}
DocumentTitle.propTypes = {
  title: PropTypes.string.isRequired
};
function reducePropsToState(propsList) {
  var innermostProps = propsList[propsList.length - 1];
  if (innermostProps) {
    return innermostProps.title;
  }
}
function handleStateChangeOnClient(title) {
  document.title = title || '';
}
export default withSideEffect(
  reducePropsToState,
  handleStateChangeOnClient
)(DocumentTitle);
React Helmet is a popular library for managing changes to the document head, such as title and meta tags, in React applications. It provides a declarative API for managing side effects related to the document head, similar to react-clientside-effect, but is more focused on head management specifically.
use-sidecar is a library that allows you to manage side effects in React components by using a sidecar pattern. It provides a way to separate side effects from the main component logic, similar to react-clientside-effect, but with a different approach to handling side effects.
FAQs
Create components whose prop changes map to a global side effect
The npm package react-clientside-effect receives a total of 1,519,546 weekly downloads. As such, react-clientside-effect popularity was classified as popular.
We found that react-clientside-effect demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.

Research
/Security News
Socket researchers found 10 typosquatted npm packages that auto-run on install, show fake CAPTCHAs, fingerprint by IP, and deploy a credential stealer.