A helper function to attach onmount handler to stateless function components
npm install react-compose-onmountreact-compose-onmount
===================


A helper function to attach onmount handler to
stateless function components.
Background
----------
If you prefer to write a React components as a function
like the following, you tend to avoid using class-based components.
``javascript`
const Hello = ({ name }) => (Hello, {name}!);
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.componentDidMount
This package is for the specific case when you want to only attach handler to a stateless function component.
Install
-------
`bash`
npm install react-compose-onmount --save
Usage
-----
Basic usage:
`javascript
import React from 'react';
import { composeWithOnMount } from 'react-compose-onmount';
const onMount = () => window.alert('mount!');
const SomePage = composeWithOnMount(onMount)(() => (
With unmount:
`javascript
const onMount = () => window.alert('mount!');
const onUnmount = () => window.alert('unmount!');const SomePage = composeWithOnMount(onMount, onUnmount)(() => (
Some Page
));
`With options:
`javascript
const onMount = () => window.alert('mount!');
const onUnmount = () => window.alert('unmount!');
const options = {
onMount: 'componentWillMount',
onUnmount: 'componentDidUnmount',
};const SomePage = composeWithOnMount(onMount, onUnmount, options)(() => (
Some Page
));
`Example
-------
The example folder contains a working example.
You can run it with
`bash
PORT=8080 npm run example
``and open