Bug report
Calling helpers.setValue
results in an infinite loop when it's called inside useEffect in React 18.
React 17: https://codesandbox.io/s/formik-usefield-hook-react-18-bug-forked-ucpg4d?file=/src/App.js
React 18: https://codesandbox.io/s/formik-usefield-hook-react-18-bug-nr052g?file=/src/App.js
The code of the App
component is exactly the same on both sandboxes, what changes is the React major version.
Expected behavior
React 17: If you click on the CLICK
button in the React 17 sandbox, you will see that the input value will change from [email protected]
to [email protected]
๐ โ
Current Behavior
React 18: If you click on the CLICK
button in the React 18 sandbox, it will cause an infinite loop โ ๏ธโ
Suggested solution(s)
I don't know exactly what could be the root cause of this issue, but I suspect that it's related to the Automatic Batching feature/breaking change introduced by React 18:
https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#automatic-batching
What happens in practice is that the reducer for SET_FIELD_VALUE
is never called for some reason, even though it's action gets dispatched within the setFieldValue
function.
The reducer for SET_ISVALIDATING
is executed instead.
On React 17:
- Formik's
setFieldValue
is called;
- dispatch
SET_FIELD_VALUE
- Reducer for
SET_FIELD_VALUE
is executed
validateFormWithHightPriority
is then executed โ
On React 18:
- Formik's
setFieldValue
is called;
- dispatch
SET_FIELD_VALUE
validateFormWithHightPriority
is executed โ
- Reducer for
SET_ISVALIDATING
is executed
- Reducer for
SET_FIELD_VALUE
is never executed for some reason โ ๏ธ
The infinite loop is then caused by the useField
hook always returning the old value instead of the new value that was set through helpers.setValue(value)
.



Your environment
| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 2.2.9
| React | 18.2.0
| TypeScript | -
| Browser | Chrome
| npm/Yarn | Yarn
| Operating System | Mac OS X