[examples] Update ReduxExample (#4267)

Using two new utils:
1. `createNavigationPropConstructor`, which is now preferred over `createReduxBoundAddListener`.
2. `initializeListeners`, which is necessary to trigger the event listeners with the initial state.
This commit is contained in:
Ashoat Tevosyan
2018-05-23 13:20:14 -04:00
committed by Brent Vatne
parent 046a9f8930
commit f6c47a6c66
2 changed files with 13 additions and 18 deletions

View File

@@ -2,11 +2,12 @@ import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { StackNavigator } from 'react-navigation';
import { initializeListeners } from 'react-navigation-redux-helpers';
import LoginScreen from '../components/LoginScreen';
import MainScreen from '../components/MainScreen';
import ProfileScreen from '../components/ProfileScreen';
import { addListener } from '../utils/redux';
import { navigationPropConstructor } from '../utils/redux';
export const AppNavigator = StackNavigator({
Login: { screen: LoginScreen },
@@ -20,17 +21,14 @@ class AppWithNavigationState extends React.Component {
nav: PropTypes.object.isRequired,
};
componentDidMount() {
initializeListeners('root', this.props.nav);
}
render() {
const { dispatch, nav } = this.props;
return (
<AppNavigator
navigation={{
dispatch,
state: nav,
addListener,
}}
/>
);
const navigation = navigationPropConstructor(dispatch, nav);
return <AppNavigator navigation={navigation} />;
}
}

View File

@@ -1,15 +1,12 @@
import {
createReactNavigationReduxMiddleware,
createReduxBoundAddListener,
createNavigationPropConstructor,
} from 'react-navigation-redux-helpers';
const middleware = createReactNavigationReduxMiddleware(
"root",
state => state.nav,
'root',
state => state.nav
);
const addListener = createReduxBoundAddListener("root");
const navigationPropConstructor = createNavigationPropConstructor('root');
export {
middleware,
addListener,
};
export { middleware, navigationPropConstructor };