Created
October 5, 2017 08:46
-
-
Save shafayeatsumit/6b97d9d2554567d02acc20c07e729c7e to your computer and use it in GitHub Desktop.
Intregating redux with react navigation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AppRegistry } from 'react-native'; | |
import { StackNavigator } from 'react-navigation'; | |
import LoginScreen from './src/components/LoginScreen'; | |
import { combineReducers, createStore, applyMiddleware, compose } from 'redux' | |
import React from 'react'; | |
import { connect, Provider, store } from 'react-redux'; | |
// import App from './src/app'; | |
// AppRegistry.registerComponent ('finderios_redux', () => App); | |
import { addNavigationHelpers } from 'react-navigation'; | |
const AppNavigator = StackNavigator({ | |
Login : { screen: LoginScreen } | |
}, { | |
// Default config for all screens | |
headerMode: 'none', | |
initialRouteName: 'LoginScreen' | |
}) | |
const tempAction = AppNavigator.router.getActionForPathAndParams('Login') | |
console.log("temp action ",tempAction) | |
const initialState = AppNavigator.router.getStateForAction(tempAction); | |
console.log("initial state",initialState) | |
const navReducer = (state = initialState, action) => { | |
console.log("action berfore nextState",action) | |
const nextState = AppNavigator.router.getStateForAction(action, state); | |
console.log("next state",nextState) | |
// Simply return the original `state` if `nextState` is null or undefined. | |
return nextState || state; | |
}; | |
const appReducer = combineReducers({ | |
nav: navReducer | |
}); | |
class App extends React.Component { | |
render() { | |
console.log("props",this.props) | |
return ( | |
<AppNavigator navigation={addNavigationHelpers({ | |
dispatch: this.props.dispatch, | |
state: this.props.nav, | |
})} /> | |
); | |
} | |
} | |
const mapStateToProps = (state) => ({ | |
nav: state.nav | |
}); | |
const AppWithNavigationState = connect(mapStateToProps)(App); | |
class Root extends React.Component { | |
render() { | |
return ( | |
<Provider store={createStore(appReducer)}> | |
<AppWithNavigationState /> | |
</Provider> | |
); | |
} | |
} | |
AppRegistry.registerComponent ('finderios_redux', () => Root); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment