0.0.3 • Published 5 years ago

mtarh-cbind v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

MTarh CBind

Travis (.com) Codecov GitHub code size in bytes

Overview

The library allows to avoid creating multiple callbacks (memorize feature).

The mtarhCBind(callback[, context])([callbackArguments]) always returns the same pointer, it does not create new functions for new calls even if arguments are different.

Examples

function foo(a) {
    return a + 1;
}

const f1 = mtarhCBind(foo)(1);
const f2 = mtarhCBind(foo)(2);

f1(); // returns 2
f2(); // returns 3
f1 === f2; // true

React use cases

const MyComponent = () => (
    <div onClick={() => {}}>Click me</div>
)

Each time when MyComponent renders it creates a new callback for "onClick" method. We can avoid it:

const MyComponent = () => (
    <div onClick={mtarhCBind(() => {})()}>Click me</div>
)

It allows to send custom arguments to callback. It merges previosly binded arguments with passed.

const handleElementClick = (event, index) => {};

const MyComponent = () => (
    <div>
        {elements.map((element, index) => (
            <li onClick={mtarhCBind(handleElementClick)(index)}>element</li>
        ))}
    </div>
)