Created
August 10, 2017 05:33
-
-
Save chj1768/c8fe8c176caad2262441aa5260d04dcb to your computer and use it in GitHub Desktop.
how to use redux in react and react-router(2.8.1)
This file contains 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 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 ); |
This file contains 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 * 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; | |
} | |
} |
This file contains 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
// 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> | |
} | |
} | |
This file contains 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 { 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