npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

memoized-bind

v1.0.1

Published

Minimal memoized bind function for React

Downloads

6

Readme

memoized-bind

Minimal memoized bind function for React

memoized-bind produces results identical to Function.prototype.bind(), except it memoizes the results for future use. Calling memoized-bind repeatedly with the same arguments will always return the same bound function. This can be especially useful when passing functions as props to child components in React.

Installation

npm install memoized-bind

Usage

memoizedBind(context)

Initializes an instance of memoized-bind.

Returns a bind function that is itself bound to the context passed to it.

bind(fn, [...args])

The return value of memoizedBind(context).

Returns a bound version of the passed in function where the this keyword is set to the context that bind was initialized with.

The results of bind are memoized. Calling bind repeatedly with the same arguments always returns the same bound function.

Example

import memoizedBind from 'memoized-bind'

const bind = memoizedBind()  // setting `this` to undefined

const multiply = (...nums) => nums.reduce((a, c) => a * c)

const boundMult = bind(multiply, 2, 3)  // Identical to multiply.bind(undefined, 2, 3)
const boundMult2 = bind(multiply, 2, 3)  // Returns the same function

const funcsStrictlyEqual = boundMult === boundMult2  // true

boundMult(7)  // 42

React Example

import React, { Component } from 'react'
import memoizedBind from 'memoized-bind'


class List extends Component {
  constructor(props) {
    super(props)
    this.state = {
      clicks: 0
    }
    this.bind = memoizedBind(this)  // sets the context of all subsequently bound functions to the component's `this`
  }

  handleItemClicked(item) {
    console.log(`${item.label} was clicked!`)
    this.setState(prevState => ({ clicks: prevState.clicks + 1 }))
  }

  render() {
    return (
      <ul>
        {this.props.items.map(item => (
          <li onClick={this.bind(this.handleItemClicked, item)} key={item.id}>{item.label}</li>
        ))}
      </ul>
    )
  }
}

Why memoized-bind?

When dealing with arrays of items in React, you often want to be able to call a method when a given item is clicked or interacted with in some way. Many times you want to call a method of the component which is generating the items. The trick is getting a reference to the particular item you want. The two common ways of doing it are passing along the item's id to the child component, or using a an inline arrow function or bound function. The problem with passing id down to the child component and then back up to the parent is that it means your child component is dealing with the parent's concerns. It has more information than it should have and is more tightly tied to its parent than it should be. The problem with using an inline arrow function or bound function is that you create a new function instance on each render, causing unecessary rerenders when using PureComponent or shouldComponentUpdate and unecessary garbage collection.

memoized-bind solves this problem by allowing you to use bind functions inline, keeping component concerns separated and preventing unecessary rerenders and garbage collection.

But why memoized-bind in particular?

There are a number of memoization libraries out there, but memoized-bind is ideal for use in React applications.

  • It's designed to be used with the component lifecycle.
    • When an instance of memoized-bind is initialized in a component's constructor, it will only exist for as long as the the component exists, providing a simple solution to memory leaks.
  • It's short and sweet.
    • The entire module is only 10 lines, 391 bytes uncompressed.
  • It's fast.
    • memoized-bind is 7 times faster than memoize-bind. See this jsPerf

License

MIT

Acknowledgements

memoize-bind

pure-bind