Skip to content

Bug: useReducer, reducer function gets called twice (possible memory leak) #21416

Closed
@hosseini44444

Description

@hosseini44444

React version: 17.0.2

Steps To Reproduce

  1. Create a counter reducer with a top limit (top limit is set externally using a prop or state)
  2. Return the current state if the count reaches the top limit
  3. Try to increment the counter by calling dispatch({type:'INCREMENT'}) a few times after the counter has reached the top limit
  4. Set the top limit to a greater number
  5. You'll see that after increasing the top limit, the reducer callback will be called as much as you'd tried to increment the counter in step 3 and the counter will increment that much

Link to code example: CODE PEN

The current behavior

After setting the top limit to a higher limit, the previous INCREMENT actions which are called before and the reducer have returned the intact state, will be called again and the reducer function will be called as much as you've tried before and the counter will increase that much.

The expected behavior

After setting the top limit to a higher limit, the previous INCREMENT actions which are called before, must not be called again and the Reducer function must not be called again and the counter must stay the same.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions