Skip to content

cheton/chained-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chained-function build status Coverage Status

NPM

Installation

npm install --save chained-function

Usage

let sum = 0;
const func = chainedFunction(
    function(val) {
        sum += val;
        console.log(sum); // 2
    },
    function(val) {
        sum += val;
        console.log(sum); // 4
    },
    function(val) {
        sum += val;
        console.log(sum); // 6
    }
);

func(2);

React

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import chainedFunction from 'chained-function';

class extends Component {
    static propTypes = {
        onClick: PropTypes.func
    };
    static contextTypes = {
        $parent: PropTypes.shape({
            onClick: PropTypes.func
        })
    };

    actions = {
        handleClick: (event) => {
        }
    }

    render() {
        const { onClick, ...props } = this.props;
        const parent = this.context.$parent;

        <a
            {...props}
            onClick={chainedFunction(
                this.actions.handleClick,
                onClick,
                parent && parent.onClick
            )}
        />
    }
}

License

MIT