Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Child components
- import Header from './Header';
- // modules for a redux-connected React component
- import React from 'react';
- import { connect } from 'react-redux';
- // extract appName from state
- const mapStateToProps = state => ({
- appName: state.appName,
- redirectTo: state.common.redirectTo
- });
- const mapDispatchToProps = dispatch => ({
- onRedirect: () =>
- dispatch({ type: 'REDIRECT' })
- });
- // render the appname
- class App extends React.Component {
- // handle redirect if necessary
- componentWillReceiveProps(nextProps) {
- // need to redirect
- if (nextProps.redirectTo) {
- // redirect to given route
- this.context.router.replace(nextProps.redirectTo);
- // update redirectTo in state
- this.props.onRedirect();
- }
- }
- render() {
- return (
- // render Header and children from router below that
- <div>
- <Header appName={this.props.appName} />
- {this.props.children}
- </div>
- );
- }
- }
- // ensure react-router attaches children to this component's props
- App.contextTypes = {
- router: React.PropTypes.object.isRequired
- };
- // export redux-connected App
- export default connect(mapStateToProps, mapDispatchToProps)(App);
Advertisement
Add Comment
Please, Sign In to add comment