Two-way binding will make your React code better.
Tags: javascript, react, mlyn, redux
Published: 2022-01-14 15:14:42

Two-way binding allows to create synchronisation between 2 entities, for example application data and view. React out of the box, provides an api to get one-way binding. When we want to mutate the state, we need explicitly call updating callback:
```ts
const UserName = ({ name, onChange }) => {
  return <input onChange={onChange} value={name} />;
};

const App = () => {
  const [user, setUser] = useState({ name: "" });
  return <UserName
    name={name}
    onChange={(e) => setUser({ name: e.target.value})}
  />;
};
```
This is done to provide owner to child update experience. So when the root state of the app gets updated, changes will propagate to children. This makes application data flow clear and predictable, however increases the amount of code to write.
In order to make two-way binding match with react update philosophy, I've built the library called `mlyn`. The main paradigm is that every piece of the state is readable and writable. However when you write to it, the change will bubble to the root of the state, and the root state will be updated:
```ts
// trailing $ suggests that the value is observable
const UserName = ({ name$ }) => {
  return <Mlyn.Input bindValue={name$} />;
};

const App = () => {
  const user$ = useSubject({ name: "" });
  return <UserName name$={user$.name} />;
};
```
That's it, the engine will update the state in the same way as on the plain react example above.
![User name update](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbl2hw04iayx6qjn879u.png)

However two-way binding is not limited to the communication with UI. You can easily bind your value to the local storage. Let say you have a hook that accepts a portion of mlyn state, and target local storage key:
```ts
const useSyncronize = (subject$, key) => {
  useEffect(() => {
    // if state exists write that to the state.
    if (localStorage[key]) { 
      subject$(JSON.parse(localStorage[key]));
    }
  }, []);
  useMlynEffect(() => {
    // whenever state is updated, update localStorage
    localStorage[key] = JSON.stringify(subject$());
  });
};
```
Now you can easily bind user name to it:
```ts
useSyncronize(user$.name, "userName");
```
Note that you don't need to create/pass any callbacks to update the value, it just works.
Another use-case is when you want to make changes of the state undoable / re-doable. Once again, just pass this state to the appropriate history management hook.
```ts
const history = useHistory(state$.name);
```
The `history` object will contain the api to jump to any step of the state. However, it's a bit customized 2-way binding. Whenever state gets updated, the value is pushed to the history:
![Snapshots are pushed to the history](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81ijenpub48vfk2k0ce9.png)
When a history entry is selected, this entry is written back to the state:
![Snapshot to state flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y148b5stm3sy0th9f8qh.png)

And note again that we don't write custom boilerplate for the state update, just connecting the dots.

Check this [code sandbox](https://codesandbox.io/s/react-mlyn-todo-mvc-with-history-lr34k?file=/src/App.js:1514-1555) with history management for a TodoMVC app:
![TodoMVC with history](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5yd1lcq1i9rg41grae2.gif)

For more examples on 2-way binding and `mlyn` visit [react-mlyn repo](https://github.com/vaukalak/react-mlyn).