react-navigation / redux-helpers

Redux middleware and utils for React Navigation
Other
296 stars 43 forks source link

Calling add listeners from props #83

Closed hoangpham95 closed 5 years ago

hoangpham95 commented 5 years ago

Following with the newest change in react-navigation 3.x, I was unable to add listener from the main navigator.

Code:

@connect((state: AppState) => {
    return {
        nav: state.NavigationState,
    };
})
export default class AppWithNavigationState extends React.Component {
    constructor(props: AppWithNavigationStateProps) {
        super(props);
            this._didFocusSubscription = props.nav.addListener("didFocus", (_payload: any) =>
            BackHandler.addEventListener(this.AndroidHardwareBackPressEvent, this._onHardwareBackPress)
        );
    }
}

After running this code, I got the error that props.nav.addListener is not a function. How should I resolve this issue?

Ashoat commented 5 years ago

You're confusing your navigation state (stored in Redux) with the React Navigation navigation prop (provided by navigators). Redux stores generally aren't supposed to have any functions in them, so trying to call a function on your state should be a red flag to you that something is off.

You might want to start by giving the Redux documentation a read. Then give the React Navigation docs about the navigation prop a read.