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-mixin

v5.0.0

Published

![travis](https://travis-ci.org/brigand/react-mixin.svg)

Downloads

78,936

Readme

travis

Note: mixins are basically dead. Only use this as a migration path for legacy code. Prefer High Order Components.

Want to use ES6/CoffeeScript/TypeScript/{InsertNoun}Script classes, and mixins?

React doesn't have anything built in for this, but don't worry! This package implements react's mixin strategy for arbitrary objects.

Install with one of:

# recommended
npm install --save react-mixin@2

# will expose window.reactMixin or the reactMixin AMD module
curl 'wzrd.in/standalone/react-mixin@2' > vendor/react-mixin.js

Here's an example:

var reactMixin = require('react-mixin');
var someMixin = require('some-mixin');
class Foo extends React.Component {
    render: function(){ return <div /> }    
}
reactMixin(Foo.prototype, someMixin);
reactMixin(Foo.prototype, someOtherMixin);

Note: Version 5.0 switches to UNSAFE_

The following methods are deprecated in React 16.3.0.

  • UNSAFE_componentWillMount
  • UNSAFE_componentWillReceiveProps
  • UNSAFE_componentWillUpdate

In version 5.0 of this plugin, the previously named componentWillMount and friends will no longer behave correctly, and instead UNSAFE_componentWillMount must be specified in the mixin.

A new utility has been added that can convert a mixin to this format.

var reactMixin = require('react-mixin');
var toUnsafe = require('react-mixin/toUnsafe');
var someMixin = require('some-mixin');

var fixedMixin = toUnsafe(someMixin);

reactMixin(Foo.prototype, fixedMixin);

Aside: Do I need mixins?

90% of the time you don't need mixins, in general prefer composition via high order components. For the 10% of the cases where mixins are best (e.g. PureRenderMixin and react-router's Lifecycle mixin), this library can be very useful.

If you do need mixins, using this library lets you avoid thinking about the merging of conflicting methods, and other oddities of react's mixin system.


Class level behavior

Many of the things that were regular properties in createClass are now static properties of the class. To have things like getDefaultProps, propTypes, and getInitialState working correctly you need to apply react-mixin a level higher than the prototype: the class itself.

var mixin = {
  getDefaultProps: function(){
    return {b: 2};
  }
};

class Foo {
  static defaultProps = {
    a: 1
  };
  render(){
    console.log(this.props); // {a: 1, b: 2}
  }
}

reactMixin.onClass(Foo, mixin);

But it's at the end of the file!

For more readability, there is an es7 decorator proposal. With the latest babel version and the stage config option set to 0 or 1, you can use decorators.

@reactMixin.decorate(mixin)
class Foo {
  static defa...
}

Note that this does prototypical inheritance, meaning the returned class is a new class rather than mutating Foo.

Differences from createClass

@ndout resolved the differences by adding reactMixin.onClass. If there are any more incompatibilites, other than autobinding methods which is intentionally omitted, please create an issue.


That's pretty much it. You get errors instead of silently overwriting things, like in react, with the exception of things whitelisted in index.js as type MANY, MANY_MERGED (getDefaultProps/getInitialState).

Autobinding is done by React.createClass, and there's no equivilent in ES6 classes. This also has better performance (I think), but you do lose some convenience. You can explicitly bind things in the constructor or componentWillMount. On the main class, the constructor replaces componentWillMount.

class Foo extends React.Component {
    constructor(props){
        super(props);
        this.handleChange = this.handleChange.bind(this);
    }
    ...
}

But... autobinding!

If you need autobinding because a mixin depends on it, you can bind the needed methods in the constructor, or do something like this (haven't given it much thought, suggestions welcome).

function autobind(methodNames){
    return {
        componentWillMount: function(){
            methodNames.forEach((name) => {
                this[name] = this[name].bind(this);
            });
        }
    };
}

@reactMixin.decorate(mixin)
@reactMixin.decorate(autobind(Object.keys(mixin)))
class Foo {
  ...
}

Like this but want to use it outside of react? See smart-mixin and define your own mixin spec.

Should I use es6 classes?

It seems to be the future with createClass becoming legacy. It's best if everyone uses one pattern for better or worse. createClass is being removed from React core in 16.0.0, but you can still install it as a separate package.