A helper function to attach onmount handler to stateless function components.
If you prefer to write a React components as a function like the following, you tend to avoid using class-based components.
const Hello = <div>Hello name!</div>;
Function components lack local state, lifecycle methods, and so on.
Local state is sometimes important to keep UI state, for example,
a string in a text field while typing, which shouldn't be put in global state.
For the local state, react-compose-state should help.
Another case is when you want to do something when a component appears.
For example, you might need to fetch data from network.
With class-based components, you can use the
componentDidMount lifecycle method.
This package is for the specific case when you want to only attach
componentDidMount handler to a stateless function component.
npm install react-compose-onmount --save
;;const onMount = window;const SomePage =<div><h1>Some Page</h1></div>;
const onMount = window;const onUnmount = window;const SomePage =<div><h1>Some Page</h1></div>;
const onMount = window;const onUnmount = window;const options =onMount: 'componentWillMount'onUnmount: 'componentDidUnmount';const SomePage =<div><h1>Some Page</h1></div>;
The example folder contains a working example. You can run it with
PORT=8080 npm run example
and open http://localhost:8080 in your web browser.