# react-addons-linked-state-mixin

> >**Note:** >This is a legacy React addon, and is no longer maintained. > >We don't encourage using it in new code, but it exists for backwards compatibility. >The recommended migration path is to explicitly set `value` and the `onChange` handler instead

Latest version **15.6.2** (published 2017-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-addons-linked-state-mixin
pnpm add react-addons-linked-state-mixin
yarn add react-addons-linked-state-mixin
bun add react-addons-linked-state-mixin
```

## Health

**Score 48/100 (D)** — status: abandoned.

Positive: has types package; no vulnerabilities; high maintenance score; high quality score; popular repo; extremely popular.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 15.6.2 |
| Published | 2017-09-26 |
| First published | 2015-07-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/react-addons-linked-state-mixin) |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 252878 |
| Maintainers | sophiebits, brianvaughn, fb |
| Keywords | react, react-addon |

## Links

- npm: https://www.npmjs.com/package/react-addons-linked-state-mixin
- Repository: https://github.com/facebook/react
- Homepage: https://github.com/facebook/react#readme
- Issues: https://github.com/facebook/react/issues
- npm.io page: https://npm.io/package/react-addons-linked-state-mixin

## Dependencies (2)

- [fbjs](https://npm.io/package/fbjs.md) ^0.8.4
- [object-assign](https://npm.io/package/object-assign.md) ^4.1.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 15.6.2 (latest) — 2017-09-26
- 15.6.0-rc.0 (15-next) — 2017-06-13
- 15.5.0-rc.3 (dev) — 2017-04-06
- 15.6.0 — 2017-06-14
- 15.6.0-rc.1 — 2017-06-01
- 15.5.2 — 2017-04-09
- 15.5.1 — 2017-04-07
- 15.5.0-rc.2 — 2017-04-06
- 15.5.0-rc.1 — 2017-04-05
- 15.5.0 — 2017-03-31
- 15.5.0-alpha.0 — 2017-03-27
- 16.0.0-alpha.3 — 2017-02-23
- 16.0.0-alpha.2 — 2017-02-09
- 16.0.0-alpha.0 — 2017-01-25
- 16.0.0-alpha — 2017-01-10
- … 47 more at https://npm.io/package/react-addons-linked-state-mixin/versions

## README

# react-addons-linked-state-mixin

>**Note:**
>This is a legacy React addon, and is no longer maintained.
>
>We don't encourage using it in new code, but it exists for backwards compatibility.  
>The recommended migration path is to explicitly set `value` and the `onChange` handler instead of using `LinkedStateMixin`.

**Importing `LinkedStateMixin`**

```javascript
import LinkedStateMixin from 'react-addons-linked-state-mixin'; // ES6
var LinkedStateMixin = require('react-addons-linked-state-mixin'); // ES5 with npm
```

If you prefer a `<script>` tag, you can get it from `React.addons.LinkedStateMixin` with:

```html
<!-- development version -->
<script src="https://unpkg.com/react-addons-linked-state-mixin/react-addons-linked-state-mixin.js"></script>

<!-- production version -->
<script src="https://unpkg.com/react-addons-linked-state-mixin/react-addons-linked-state-mixin.min.js"></script>
```

In this case, make sure to put the `<script>` tag after React.

**Importing `LinkedInput`**

After React 16, you will also need `LinkedInput` component if you want to keep using this pattern. You can import it like this:

```javascript
import LinkedInput from 'react-linked-input'; // ES6
var LinkedInput = require('react-linked-input'); // ES5 with npm
```

If you prefer a `<script>` tag, you can get it from `LinkedInput` global with:

```html
<!-- development version -->
<script src="https://unpkg.com/react-linked-input/react-linked-input.js"></script>

<!-- production version -->
<script src="https://unpkg.com/react-linked-input/react-linked-input.min.js"></script>
```

## Overview

`LinkedStateMixin` is an easy way to express two-way binding with React.

In React, data flows one way: from owner to child. We think that this makes your app's code easier to understand. You can think of it as "one-way data binding."

However, there are lots of applications that require you to read some data and flow it back into your program. For example, when developing forms, you'll often want to update some React `state` when you receive user input. Or perhaps you want to perform layout in JavaScript and react to changes in some DOM element size.

In React, you would implement this by listening to a "change" event, read from your data source (usually the DOM) and call `setState()` on one of your components. "Closing the data flow loop" explicitly leads to more understandable and easier-to-maintain programs. See [our forms documentation](https://facebook.github.io/react/docs/forms.html) for more information.

Two-way binding -- implicitly enforcing that some value in the DOM is always consistent with some React `state` -- is concise and supports a wide variety of applications. We've provided `LinkedStateMixin`: syntactic sugar for setting up the common data flow loop pattern described above, or "linking" some data source to React `state`.

> Note:
>
> `LinkedStateMixin` is just a thin wrapper and convention around the `onChange`/`setState()` pattern. It doesn't fundamentally change how data flows in your React application.

## LinkedStateMixin: Before and After

Here's a simple form example without using `LinkedStateMixin`:

```javascript
var createReactClass = require('create-react-class');

var NoLink = createReactClass({
  getInitialState: function() {
    return {message: 'Hello!'};
  },
  handleChange: function(event) {
    this.setState({message: event.target.value});
  },
  render: function() {
    var message = this.state.message;
    return <input type="text" value={message} onChange={this.handleChange} />;
  }
});
```

This works really well and it's very clear how data is flowing, however, with a lot of form fields it could get a bit verbose. Let's use `LinkedStateMixin` to save us some typing:

```javascript
var createReactClass = require('create-react-class');
var LinkedInput = require('react-linked-input');

var WithLink = createReactClass({
  mixins: [LinkedStateMixin],
  getInitialState: function() {
    return {message: 'Hello!'};
  },
  render: function() {
    return <LinkedInput type="text" valueLink={this.linkState('message')} />;
  }
});
```

`LinkedStateMixin` adds a method to your React component called `linkState()`. `linkState()` returns a `valueLink` object which contains the current value of the React state and a callback to change it. The `LinkInput` component passes those properties to the input it renders.

`valueLink` objects can be passed up and down the tree as props, so it's easy (and explicit) to set up two-way binding between a component deep in the hierarchy and state that lives higher in the hierarchy.

Note that checkboxes have a special behavior regarding their `value` attribute, which is the value that will be sent on form submit if the checkbox is checked (defaults to `on`). The `value` attribute is not updated when the checkbox is checked or unchecked. For checkboxes, you should use `checkedLink` instead of `valueLink`:

```
<LinkedInput type="checkbox" checkedLink={this.linkState('booleanValue')} />
```

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