react-mixin-safe-state-change

0.1.1 • Public • Published

react-mixin-safe-state-change

npm version npm downloads

React mixin which sets or replaces state only when it is safe to do so.

Motivation

When processing the response of an asynchronous request, it might not be safe to call setState or replaceState, because the component might no longer be mounted.

Install

npm install react-mixin-safe-state-change

Usage

var safeStateChangeMixin = require('react-mixin-safe-state-change');

React.createClass({
  mixins: [safeStateChangeMixin]

  someCallback: function(newValue) {
    if (!this.setStateSafe({value: newValue})) {
      console.warn('Could not set the state.');
    }

    // or

    if (!this.replaceStateSafe({value: newValue})) {
      console.warn('Could not replace the state.');
    }
  }
});

Readme

Keywords

Package Sidebar

Install

npm i react-mixin-safe-state-change

Weekly Downloads

1

Version

0.1.1

License

MIT

Last publish

Collaborators

  • rickbeerendonk