Skip to content

Bug: componentWillUnmount is called twice #24280

@mgomulak

Description

@mgomulak

React version: 18.0.0

Steps To Reproduce

componentWillUnmount is called twice upon toggling the rendered component. Even when StrictMode is disabled

Link to code example: https://codesandbox.io/s/componentwillunmount-called-twice-hrpzy5?file=/src/App.js

The current behavior

After upgrading to react 18 we've seen some different behavior in a conditionally rendered, lazy class component.

In the provided code example the class component is rendered first. After the first toggle, the class component's componentWillUnmount is called twice.

Subsequent toggle calls correctly lead to a single componentWillUnmount invocation.

This does only seem to affect the class component when its rendered first. If the condition is changed to initially show the other function component the class component unmounts just fine

The expected behavior

The class component's componentWillUnmount is only called once

Metadata

Metadata

Assignees

No one assigned

    Labels

    React 18Bug reports, questions, and general feedback about React 18Type: Bug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions