react: Bug: Bad state updates with Promise.resolve and useReducer

React version: 18.1.0

Steps To Reproduce

Code Example

import React, { useEffect, useReducer } from 'react'

function reducer(state, action) {
  switch (action.type) {
    case 'CHANGE':
      return {
        ...state,
        values: {
          ...state.values,
          [action.field]: action.value,
        }
      }
    case 'SUBMIT':
      return {
        ...state,
        submit: !state.submit,
      }
    default:
      return state
  }

}

function App() {
  const [state, dispatch] = useReducer(reducer, {
    submit: false,
    values: {
      foo: 'hello'
    }
  })

  useEffect(() => {
    dispatch({ type: 'SUBMIT' })
    Promise.resolve().then(() => {
      dispatch({ type: 'SUBMIT' })
    })
  }, [state.values])

  return (
    <div className="App">
      <input
        type="text"
        value={state.values.foo}
        onChange={(e) => {
          dispatch({ type: 'CHANGE', field: 'foo', value: e.target.value })
        }}
      />
    </div>
  )
}

export default App

I think is the same bug of #24625 But i fill this issue because this scenario breaks a lot of library out of there i found this bugs using https://github.com/jaredpalmer/formik If you type fast or you slowdown your cpu using dev tools you see the page freeze and you will see: Warning: Maximum update depth exceeded.

The current behavior

Not seeing Warning: Maximum update depth exceeded and freeze

The expected behavior

See Warning: Maximum update depth exceeded and freeze

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 17 (3 by maintainers)

Most upvoted comments

See also #24649. I’ve actually discovered it because of Maximum update depth exceeded warning, although I decided not to replicate it in my example. But the root cause is likely the same.

@gffuma My assumption is that it’s related to Automatic Batching which was introduced with React 18.

I tried debugging Formik’s code to understand what happens. If you look at my issue here you will that for this Formik code:

image

image

works like this with React 17:

  • Formik’s setFieldValue is called;
  • dispatch SET_FIELD_VALUE
  • Reducer for SET_FIELD_VALUE is executed (no batching)
  • validateFormWithHightPriority is then executed ✅

Whereas, with React 18:

  • Formik’s setFieldValue is called;
  • dispatch SET_FIELD_VALUE
  • validateFormWithHightPriority is executed ❗(I guess it’s because of batching, React doesn’t execute the state reducer for SET_FIELD_VALUE right away as it used to do in React 17)
  • Reducer for SET_ISVALIDATING is executed
  • Reducer for SET_FIELD_VALUE is never executed for some reason ⚠️.

It’s a tiny implementation detail, but I think it potentially affect other use cases and therefore should be treated as a React 18 breaking change, even though they mention that upgrading from version 17 to version 18 should happen seamlessly.

The Formik team could address the issue and come up with a workaround, but I agree with you, the main issue is with React 18.

Do you think this issue could be related to the following one?

https://github.com/jaredpalmer/formik/issues/3602