Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
react-side-effect
Advanced tools
The react-side-effect package is a utility for creating higher-order components that handle side effects in React applications. It allows you to manage side effects in a declarative way, ensuring that they are handled consistently and predictably.
Creating a Side Effect Component
This feature allows you to create a higher-order component that manages side effects. The `reducePropsToState` function combines all props into a single state object, and the `handleStateChangeOnClient` function handles the side effect on the client.
const withSideEffect = require('react-side-effect');
function reducePropsToState(propsList) {
// Combine all props into a single state object
return propsList.reduce((acc, props) => ({ ...acc, ...props }), {});
}
function handleStateChangeOnClient(state) {
// Handle the side effect on the client
console.log('State changed:', state);
}
const SideEffectComponent = withSideEffect(reducePropsToState, handleStateChangeOnClient)(MyComponent);
Using the Side Effect Component
Once you have created the side effect component, you can use it in your React application like any other component. The side effects will be managed according to the logic defined in the higher-order component.
<SideEffectComponent prop1="value1" prop2="value2" />
react-helmet is a package that manages changes to the document head, such as title and meta tags, in a declarative way. It is similar to react-side-effect in that it handles side effects, but it is specifically focused on managing the document head.
redux-saga is a middleware library for managing side effects in Redux applications. It uses generator functions to handle asynchronous actions and side effects. While it is more complex than react-side-effect, it provides powerful tools for managing side effects in a Redux-based application.
react-use is a collection of React hooks that includes hooks for managing side effects, such as useEffectOnce and useAsync. It provides a more granular approach to managing side effects compared to react-side-effect, which focuses on higher-order components.
Create components whose prop changes map to a global side effect.
npm install --save react-side-effect
createSideEffect: (onChange: Array<Props> -> ()) -> ReactComponent
Returns a component that, when mounted, unmounted or having received new props, calls onChange
with each mounted component's props
.
It's up to you to reduce
them, use innermost values, or whatever you fancy.
Component will have a static dispose()
method to clear the stack of mounted instances.
When rendering on server, you must call it after each request.
Here's how to implement React Document Title using React Side Effect:
'use strict';
var React = require('react'),
createSideEffect = require('./createSideEffect');
/**
* Extract title from a list of each mounted component's props.
* We're interested in the innermost title, but for other use cases we might want to call `propList.reduce`.
*/
function extractTitle(propsList) {
var innermostProps = propsList[propsList.length - 1];
if (innermostProps) {
return innermostProps.title;
}
}
var _serverTitle = null;
/**
* Generate a component that reacts to mounting, onmounting and prop changes by updating document title.
*/
var SetDocumentTitle = createSideEffect(function handleChange(propsList) {
var title = extractTitle(propsList);
if (typeof document !== 'undefined') {
document.title = title || '';
} else {
_serverTitle = title || null;
}
});
/**
* Create a wrapper for it with propTypes, displayName and helpers for server and testing.
*/
var DocumentTitle = React.createClass({
propTypes: {
title: React.PropTypes.string.isRequired
},
statics: {
/**
* Peek at current title (for tests).
*/
peek: function () {
return _serverTitle;
},
/**
* Call this on server after each request to get current title.
*/
rewind: function () {
var title = _serverTitle;
SetDocumentTitle.dispose();
return title;
}
},
render: function () {
return React.createElement(SetDocumentTitle, this.props);
}
});
module.exports = DocumentTitle;
FAQs
Create components whose prop changes map to a global side effect
The npm package react-side-effect receives a total of 0 weekly downloads. As such, react-side-effect popularity was classified as not popular.
We found that react-side-effect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.