Declarative side effects!
yarn add react-create-side-effect react
Creates a component that executes a side effect when component mounts. Return a function to handle any clean up when the component unmounts.
Kind: inner method of ReactCreateSideEffect
Param | Type |
---|---|
fn | function |
Example
import React from "react";
import ReactDOM from "react-dom";
import { createSideEffect } from "react-create-side-effect";
const LocationRedirect = createSideEffect(props => {
window.location.replace(props.to);
});
ReactDOM.render(
<LocationRedirect to="/login" />,
document.getElementById("root")
);
Example (cleaning up)
import React from "react";
import ReactDOM from "react-dom";
import { createSideEffect } from "react-create-side-effect";
const Body = createSideEffect(props => {
document.body.classList.add(props.className);
return () => document.body.classList.remove(props.className);
});
ReactDOM.render(<Body className="mounted" />, document.getElementById("root"));