JSPM

react-mixin-safe-state-change

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q16363F
  • License MIT

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

Package Exports

  • react-mixin-safe-state-change

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-mixin-safe-state-change) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.');
    }
  }
});