Skip to content

StackNavigator specific lifecycle should be emphasized #227

Closed
@mpyw

Description

@mpyw

Custom Android back button behavior · React Navigation

At first, you may be inclined to use componentDidMount to subscribe for the back press event and componentWillUnmount to unsubscribe. Reason why we do not use them is that they are not generally called when entering or leaving a screen.

More specifically, consider a StackNavigator with screens A and B. After navigating to A, its componentDidMount is called. When pushing B, its componentDidMount is also called, but A remains mounted and its componentWillUnmount is therefore not called.

Similarly, when going back from B to A, componentWillUnmount of B is called, but componentDidMount of A is not because A remained mounted the whole time.

This is significantly important especially when it is integrated with Redux. However, the document is placed on the improper location.

I have formed Redux states that correspond with page components one by one whose style is often adopted on the Web. But the behaviors of componentDidMount and componentWillUnmount on StackNavigator are completely different from those on the Web. I finally noticed that a state of stackable screen must be kept in an array or placed as a local state of React rather than the global state of Redux, but it was too late. I wasted a lot of time. This important information should be written in a early section.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions