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

v0.0.1

Published

A React.js Mixin with ES5 and ES6-compatible semantics which provides managed event listeners that respect component lifecycles, as well as a state setter which respects React invariants and is suitable for one-time event handlers.

Downloads

3

Readme

React Observer Mixin Build Status codecov.io Dependency Status

A React.js Mixin with ES5 and ES6-compatible semantics which provides managed event listeners that respect component lifecycles, as well as state setters which are compatible with React invariants and are suitable for use in one-time event handlers.

This module has no runtime dependencies and weighs in at ~1.3kB minified.

Why does this exist?

Asynchronous callbacks which operate on a component's state will violate React's invariants if the component is not mounted when the callback is executed. This utility offers a mixin for React Component providing the following functionality:

  • listenTo: Managed event listening respecting component lifecycles. Listeners are automatically detached when the component is un-mounted.

  • setStateIfMounted: Conditional state setting which helps to avoid a violation of React's invariants when you know that you can safely drop the result of an asynchronous request.

ES6 Example

import observer from 'react-observer-mixin';

class MyComponent extends React.Component {
  constructor(props) {
    super(props);

    // Attach mixin methods and spies necessary to support them.
    observer(this);

    // The event handler automatically detaches when this component
    // is un-mounted.
    this.listenTo(props.observable, 'event', (result) => {
      this.setState({value: result});
    });

    // Other forms of async callbacks
    ajax(props.url).then((result) => {
      this.setStateIfMounted({value: result});
    });
  }
}

ES5 Example


var ObserverMixin = require('react-observer-mixin').Mixin;

var MyComponent = React.createClass({
  mixins: [ObserverMixin]
  getInitialState: function() {

    // Method documentation in the ES6 example applies here

    this.listenTo(this.props.observable, 'eventName', function(result) {
      this.setState({value: result});  
    }.bind(this));

    ajax(this.props.url1).then(function (result) {
      this.setStateIfMounted({value2: result}); 
    }.bind(this));
  }
});

Definitions

| Term | Definition | | ---- | ---------- | | Mixin | Refers to this module's Mixin returned by the build function | | Observable | Object providing on or addEventListener methods |

API

Module

(Mixin) module.build

Generate a Mixin which can be provided to the list of mixins for a React component.

See above for example usage.

module.mixin(React.Component)

Attach Mixin methods to an React.Component built in an ES6 fashion.

Provided Mixin Methods

this.listenTo(Observable, StringOrObject, [Function])

Attaches one or more managed listeners to an observable object. The event listeners will be detached when the component is un-mounted, and re-attached if/when the component is mounted again.

Example:

  // ...
  getInitialState: function() {
    this.listenTo(this.props.element, 'change', function() {
      // ...
    });

    // For ease of use, multiple handlers may be attached by using
    // an object as the second parameter.
    this.listenTo(this.props.tcpClient, {
      connect: function () {
        // ...
      },
      disconnect: function () {
        // ...
      }
    });
  }

this.setStateIfMounted(Object)

Sets state if and only if the component is currently mounted. If the component is un-mounted, this call will be ignored and any given state will be silently dropped.

Contributors

Accounts with accepted PRs will be added to this list.

  • Chris Carpita

License

MIT