foxhound87 / mobx-react-form

Reactive MobX Form State Management
https://foxhound87.github.io/mobx-react-form
MIT License
1.09k stars 129 forks source link

fix: #632 assignable hooks #633

Closed cmolenda closed 7 months ago

cmolenda commented 7 months ago

https://github.com/foxhound87/mobx-react-form/issues/632

This bug was introduced in v6.4.0

In rare circumstances, creating a form class with a hooks method can cause the first trigger of the hook to fail.

Example:

import { Form } from "mobx-react-form";
import { makeObservable, flow } from "mobx";

let x = 0;

class HookedForm extends Form {
  constructor() {
    super({ fields: ["aField"] });
    makeObservable(this, {
      onSuccess: flow,
    });
  }

  hooks() {
    return { onSuccess: this.onSuccess };
  }

  *onSuccess() {
    alert(`onSuccess ${++x}`);
  }
}

export default function App() {
  return (
    <div className="App">
      <button onClick={() => new HookedForm().submit()}>Test</button>
    </div>
  );
}

Change the hook and handler objects to shallow observables, which prevents this problem.

github-actions[bot] commented 7 months ago

:tada: This PR is included in version 6.7.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: