react-navigation / redux-helpers

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

addListener or NavigationEvents will only work at first open. #87

Closed rabanos-rvc closed 5 years ago

rabanos-rvc commented 5 years ago

I am using bottomNavigation with stackNavigator

Screen A's navigation addListener is fine, and then if I navigate to Screen B, the navigation addListener is also working but if go back to screen A and navigate to Screen B again the addListener will not work anymore. I tried using NavigationEvents and still it is not working.

"react-navigation": "3.0.0", "react-navigation-redux-helpers": "^2.0.6", "react-navigation-slide-from-right-transition": "^1.0.4", "react-redux": "^6.0.0", "react-timer-mixin": "0.13.4", "reactotron-core-client": "^2.8.2", "redux": "^4.0.0", "redux-saga": "^1.0.0", "reduxsauce": "1.0.1",

My CreateStore.js

import { createStore, applyMiddleware, compose } from 'redux' import Config from '../Config/DebugConfig' import createSagaMiddleware from 'redux-saga' import ScreenTracking from './ScreenTrackingMiddleware' import { appNavigatorMiddleware } from '../Navigation/ReduxNavigation'

// creates the store export default (rootReducer, rootSaga) => { / ------------- Redux Configuration ------------- /

const middleware = [] const enhancers = []

/ ------------- Navigation Middleware ------------ / middleware.push(appNavigatorMiddleware)

/ ------------- Analytics Middleware ------------- / middleware.push(ScreenTracking)

/ ------------- Saga Middleware ------------- /

const sagaMonitor = Config.useReactotron ? console.tron.createSagaMonitor() : null const sagaMiddleware = createSagaMiddleware({ sagaMonitor }) middleware.push(sagaMiddleware)

/ ------------- Assemble Middleware ------------- /

enhancers.push(applyMiddleware(...middleware))

// if Reactotron is enabled (default for DEV), we'll create the store through Reactotron const createAppropriateStore = Config.useReactotron ? console.tron.createStore : createStore const store = createAppropriateStore(rootReducer, compose(...enhancers))

// kick off root saga let sagasManager = sagaMiddleware.run(rootSaga)

return { store, sagasManager, sagaMiddleware } }

ReduxNavigation.js import * as React from 'react' import { BackHandler, Platform } from 'react-native' import { createReactNavigationReduxMiddleware, reduxifyNavigator } from 'react-navigation-redux-helpers' import { connect } from 'react-redux' import AppNavigation from './AppNavigation'

export const appNavigatorMiddleware = createReactNavigationReduxMiddleware( 'root', (state) => state.nav )

const ReduxAppNavigator = reduxifyNavigator(AppNavigation, 'root')

class ReduxNavigation extends React.Component { componentDidMount () { if (Platform.OS === 'ios') return BackHandler.addEventListener('hardwareBackPress', () => { const { dispatch, nav } = this.props // change to whatever is your first screen, otherwise unpredictable results may occur if (nav.routes.length === 1 && (nav.routes[0].routeName === 'LaunchScreen')) { return false } // if (shouldCloseApp(nav)) return false dispatch({ type: 'Navigation/BACK' }) return true }) }

componentWillUnmount () { if (Platform.OS === 'ios') return BackHandler.removeEventListener('hardwareBackPress', undefined) }

render () { return } }

const mapStateToProps = state => ({ nav: state.nav }) export default connect(mapStateToProps)(ReduxNavigation)

Ashoat commented 5 years ago

Please update to the latest version of this package

rabanos-rvc commented 5 years ago

Hi, I updated it to 3.0.2 and it still not working :(

I also changed my ReduxNavigation.js file import { createReactNavigationReduxMiddleware, createReduxContainer } from 'react-navigation-redux-helpers' import { connect } from 'react-redux' import AppNavigation from './AppNavigation'

export const appNavigatorMiddleware = createReactNavigationReduxMiddleware( (state) => state.nav, 'root' )

const ReduxAppNavigator = createReduxContainer(AppNavigation, 'root')

Ashoat commented 5 years ago

I am not sure what’s wrong from the code snippets you copy-pasted. This issue seems like it might be the same as #53. That one got closed because nobody was able to provide a repro.

I can investigate this issue further if you can provide an MCVE that reproduces the issue, in the form of an Expo Snack or a react-native init'd repo hosted on GitHub.

rabanos-rvc commented 5 years ago

something wrong with my NavigationActions.navigate... gonna close this issue. Thanks