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

react-class-binder

v1.0.1

Published

Automatically binds React method when using plain ES6 classes

Downloads

1

Readme

React Class Binder

js-standard-style

Automatically bind methods defined in a React component using the ES2015 class syntax (similar to how React.createClass works).

This is a small package that uses a "trick" that allows mixin behavior in ES2015 using "subclass factories" so that you don't need to manually call the function in the constructor() function, and you don't need to resort to still unstandardized syntax like decorators or ES2015 class properties. You can read more about it here.

Installation

To install the stable version:

npm install --save react-class-binder

Usage

This package uses a cool trick with ES2015 classes to make it extremely easy to use:

import React from 'react'
import binder from 'react-class-binder'

export default class ComponentName extends binder(React.Component) {
  // ...component stuff here
}

No other configuration or options are needed! react-class-binder will only bind what is absolutely necessary, so it won't touch React related method, nor will it rebind any method on any class that extends your class. So any libraries which use inheritance or higher-order-components will work fine with react-class-binder (Including react-css-modules and radium)!

Alternate-Usage:

I also added (starting with version 1.1.0) a second way to use this if you don't want to import react-class-binder in each method. It replaces the React import with one that has BinderComponent added to it. So you can use it like this:

import React from 'react-class-binder/react'

export default class ComponentName extends React.BinderComponent {
  // ...component stuff here
}

Everything else is exactly the same. Please note that this method should be consitered beta as I haven't used it as extensively as I've used the mixin method.

Full Example

import React from 'react'
import binder from 'react-class-binder'

//                                      V   That's all that's needed!
export default class ReverseP extends binder(React.Component) {
  static propTypes = {
    text: React.PropTypes.string.isRequired
  }

  reverseText () {
    // `this` is magically bound to the correct context!
    return this.props.text.split('').reverse().join('')
  }

  // Since `this` is already "correctly bound" in React's functions, `react-class-binder` skips these
  componentDidMount () {
    console.log('Component Mounted!')
  }
  componentWillUnmount () {
    console.log('Component About to Unmount!')
  }

  render () {
    return <p>{this.reverseText()}</p>
  }
}

FAQ

Why?
I was using the reverseText = () => { trick for a while to allow my React functions to be "correctly bound", however it always rubbed me the wrong way. It was still unfinalized syntax, it was somewhat unintuitive to others that didn't know the trick, and it became annoying when I wanted to use async functions in a class once. So I made this package to avoid having to use that.

Why not just use one of the other packages for this?
One of them required decorators, which was a no-start because I'm trying to avoid non-standard syntax. A few others I saw used functions that were called in the constructor, which was a bit more boilerplate than I would have preferred to have in each component, and they didn't play well with other libraries that inherited from your class like react-css-modules. This is simple, small, and works in all cases I've tried so far.

Does this impact performance? react-class-binder does all binding when the object is instantiated (in React, this is basically the componentWillMount lifecycle method). That is the only place that will take longer when using this package, and even that is so small that I can't reliably measure it.

Inspiration & Thanks

Contributing

The code is written in ES6 using Javascript Standard Style. Feel free to make PRs adding features you want, but please try to follow Standard. Also, documentation/readme PRs are more then welcome!

License

MIT Copyright (c) Gregory Benner