Skip to content

Instantly share code, notes, and snippets.

@chj1768
Created August 10, 2017 05:33
Show Gist options
  • Save chj1768/c8fe8c176caad2262441aa5260d04dcb to your computer and use it in GitHub Desktop.
Save chj1768/c8fe8c176caad2262441aa5260d04dcb to your computer and use it in GitHub Desktop.
how to use redux in react and react-router(2.8.1)
import React from 'react';
import { connect } from 'react-redux';
import { Header } from './Header.jsx';
import { Footer } from './Footer.jsx';
class AppComponent extends React.Component {
constructor( props ) {
super( props );
}
render() {
return <div style={ { height : '100%' } }>
<Header isRender={ this.isRender }
pathName={ this.props.location.pathname }
ref="header" />
<div className='body_container'>
{ this.props.children }
</div>
{/*<Footer />*/}
</div>
}
}
export const App = connect( state => { return { login : state.authentication.login } } )( AppComponent );
import * as types from '../actions/ActionTypes.jsx';
const initialState = {
login : {
isLoggedIn: false,
currentUser : null
}
};
export default function authentication( state = initialState, action ) {
switch( action.type ) {
case types.LOGIN_SUCCESS:
return Object.assign({}, state, {
login : {
isLoggedIn : true,
currentUser : action.userId
}
});
case types.LOGIN_FAILURE:
return Object.assign({}, state, {
login : {
isLoggedIn : false,
currentUser : null
}
});
case types.LOGOUT:
return Object.assign({}, state, {
login : {
isLoggedIn : false,
currentUser : null
}
});
default:
return state;
}
}
// React Package
import React from 'react';
import { Router, Route, IndexRoute } from 'react-router';
import { Provider } from 'react-redux';
import { store, history } from './store.jsx';
// Main App Routers
import { App } from './App.jsx';
// Other Routers
import { Login } from '../../components/Account/client/Login.jsx';
import { A } from '../../components/A/client/A.jsx';
import { B } from '../../components/B/client/B.jsx';
import { C } from '../../components/C/client/C.jsx';
const requireAuth = ( nextState, replace ) => {
if( !Meteor.userId() ) {
replace( '/login' );
}
};
export class Routes extends React.Component {
constructor( props ) {
super( props );
}
render () {
return <Provider store={ store } >
<Router history={ history } >
<Route path='/' component={ App } onEnter={ requireAuth } >
<IndexRoute component={ A } />
<Route path='agency' component={ B } />
<Route path='event' component={ C } />
</Route>
<Route path='/login' component={ Login } />
</Router>
</Provider>
}
}
import { compose, createStore, applyMiddleware } from 'redux';
import { syncHistoryWithStore, routerMiddleware } from 'react-router-redux';
import { browserHistory } from 'react-router';
import { thunk } from 'redux-thunk';
import { combineReducers } from 'redux';
import { routerReducer } from 'react-router-redux';
import authentication from './Authentication.jsx';
export default combineReducers( { authentication, routing : routerReducer } );
export const store = createStore( combineReducers, compose( applyMiddleware( routerMiddleware( browserHistory ) ) ) );
export const history = syncHistoryWithStore( browserHistory, store );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment