Code Monkey home page Code Monkey logo

react-compose-onmount's Introduction

react-compose-onmount

Build Status npm version bundle size

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.

const Hello = ({ name }) => (<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.

Install

npm install react-compose-onmount --save

Usage

Basic usage:

import React from 'react';
import { composeWithOnMount } from 'react-compose-onmount';

const onMount = () => window.alert('mount!');

const SomePage = composeWithOnMount(onMount)(() => (
  <div>
    <h1>Some Page</h1>
  </div>
));

With unmount:

const onMount = () => window.alert('mount!');
const onUnmount = () => window.alert('unmount!');

const SomePage = composeWithOnMount(onMount, onUnmount)(() => (
  <div>
    <h1>Some Page</h1>
  </div>
));

With options:

const onMount = () => window.alert('mount!');
const onUnmount = () => window.alert('unmount!');
const options = {
  onMount: 'componentWillMount',
  onUnmount: 'componentDidUnmount',
};

const SomePage = composeWithOnMount(onMount, onUnmount, options)(() => (
  <div>
    <h1>Some Page</h1>
  </div>
));

Example

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.

react-compose-onmount's People

Contributors

dai-shi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.