Skip to content

Instantly share code, notes, and snippets.

@og24715
Last active May 9, 2019 01:30
Show Gist options
  • Save og24715/231c2871d30feaac34820a6f32d9526c to your computer and use it in GitHub Desktop.
Save og24715/231c2871d30feaac34820a6f32d9526c to your computer and use it in GitHub Desktop.
React Navigation with Redux Boilerplate Code

versions

version
react 16.4.1
react-native 0.56.0
react-navigation 2.9.2
redux 4.0.0
react-redux 5.0.7
import React from 'react';
import { Provider, connect } from 'react-redux';
import { reduxifyNavigator } from 'react-navigation-redux-helpers';
import pageNation from './pageNation';
import { configureStore } from './Store';
const store = configureStore();
const mapStateToProps = (state) => ({
state: state.nav,
});
const Root = reduxifyNavigator(pageNation, 'root');
const RootWithNavigationState = connect(mapStateToProps)(Root);
export default class App extends React.Component {
render() {
return (
<Provider store={store}>
<RootWithNavigationState />
</Provider>
);
}
}
import { StackNavigator } from 'react-navigation';
import Initialize from './screen/initialize';
const PageNation = StackNavigator({
initialize: Initialize,
});
export default PageNation;
import { combineReducers } from 'redux';
import { createNavigationReducer } from 'react-navigation-redux-helpers';
import pageNation from '../pageNation';
import tweets from './tweets';
import twitter from './twitter';
const navReducer = createNavigationReducer(pageNation);
export default combineReducers({
nav: navReducer,
tweets,
twitter,
});
import { createStore, applyMiddleware, compose } from 'redux';
import { createLogger } from 'redux-logger';
import thunk from 'redux-thunk';
import {
createReactNavigationReduxMiddleware,
} from 'react-navigation-redux-helpers';
import reducers from './ducks/reducers';
const reactNavigationReduxMiddleware = createReactNavigationReduxMiddleware(
'root',
state => state.nav,
);
const loggerMiddleware = createLogger({ predicate: (getState, action) => __DEV__ });
const middlewares = [reactNavigationReduxMiddleware, loggerMiddleware, thunk];
export const configureStore = () => {
const enhancer = compose(applyMiddleware(...middlewares));
const store = createStore(reducers, enhancer);
return store;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment