# statemixin

> Fixes issues with the state being set inside REST callbacks that aren't notified of the component being unmounted.

Latest version **0.1.0** (published 2014-10-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install statemixin
pnpm add statemixin
yarn add statemixin
bun add statemixin
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2014-10-18 |
| First published | 2014-10-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | charliedowler |
| Maintainers | charliedowler |

## Links

- npm: https://www.npmjs.com/package/statemixin
- Repository: https://github.com/charliedowler/StateMixin
- Issues: https://github.com/charliedowler/StateMixin/issues
- npm.io page: https://npm.io/package/statemixin

## Recent versions

- 0.1.0 (latest) — 2014-10-18

## README

## StateMixin
> Fixes issues with the state being set inside REST callbacks that aren't notified of the component being unmounted.

### Problem:
```js
var React = require('react');

var Component = React.createClass({
    mixins: [SomeAPIMixin],
    componentWillMount: function() {
        this.api.request('http://google.com', function(err, res) {
            /**
            * Ahhhh, another event has fired elsewhere in the app
            * that causes this component to unmount while we were
            * waiting for this request to come back.
            */
            this.setState({ data: res.body });
        }.bind(this);
    },
    render: function() {
        return <div> Results: {this.state.data.length} </div>;
    }
});
```

### Solution:
```js
var React = require('react');
var StateMixin = require('StateMixin');

var Component = React.createClass({
    mixins: [SomeAPIMixin, StateMixin],
    debounce: 500, // Throttle "Invariant Violation" warnings
    componentWillMount: function() {
        this.api.request('http://google.com', function(err, res) {
            /**
            * With the StateMixin the setState call is caught and
            * a check is run to see if the component is currently mounted
            * Saving you from any possible "Invariant Violation's"
            */
            this.setState({ data: res.body });
        }.bind(this);
    },
    render: function() {
        return <div> Results: {this.state.data.length} </div>;
    }
});
```

---
_Source: https://npm.io/package/statemixin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
