jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.98k stars 2.79k forks source link

useFormik hook isn't stable #3289

Open skyh opened 3 years ago

skyh commented 3 years ago

Bug report

Current Behavior

useFormik hook creates new formik instance each time being called

Expected behavior

Expected useFormik hook to return the same instance of formik object if called with the same config

Reproducible example

The counter is needed to re-render component every second. Please, check the console logs to see unstable useFormik hook result.

https://codesandbox.io/s/withformik-codesandbox-template-forked-xxpg5?file=/index.js

markm247 commented 3 years ago

This has nothing to do with the formik object being useFormik hook and will occur regardless of what you set it to (try with const formik = {}). ESLint recommends useMemo:

"The 'formik' object makes the dependencies of useEffect Hook (at line 28) change on every render. To fix this, wrap the initialization of 'formik' in its own useMemo() Hook. (react-hooks/exhaustive-deps)"

johnrom commented 3 years ago

This is done in my v3 PR.