react-router-redux 没有改变 content/view
react-router-redux does not change content/view
我将 react-router-redux 设置为 example。但是 dispatch(push(url))
不会更改地址栏上的 content/view 或 url。尽管从我的控制台我可以看到 LOCATION_CHANGE 和 CALL_HISTORY_METHOD 已使用我给定的地址成功调用。在这种情况下,如果登录成功,它不会加载预期的 redirect
地址。
签到动作
export function loginUser(email, password, redirect="/dashboard") {
return function(dispatch) {
dispatch(loginUserRequest());
return fetch(`http://localhost:3000/users/sign_in/`, {
...
})
.then(parseJSON)
.then(response => {
try {
let decoded = jwtDecode(response.token);
dispatch(loginUserSuccess(response.token));
dispatch(push(redirect));
} catch (e) {
...
}
})
}
}
routes.js
import React from 'react';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux'
...
import store from '../store';
const history = syncHistoryWithStore(browserHistory, store)
let Routes =
<Router history={history}>
<Route path='/' component={MainContainer}>
<IndexRoute component={Home} />
<Route path='/sign_in' component={SigninForm} />
<Route path='dashboard' component={authenticateComponent(Dashboard)} />
</Route>
</Router>
export default Routes;
store.js
import { createStore, applyMiddleware, compose } from 'redux'
import thunkMiddleware from 'redux-thunk'
import createLogger from 'redux-logger'
import reducers from './reducers';
const createStoreWithMiddleware = compose(
applyMiddleware(
thunkMiddleware,
createLogger()
)
)
const store = createStore(reducers, createStoreWithMiddleware);
export default store;
AuthenticateComponent.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { push } from 'react-router-redux';
export function authenticateComponent(Component) {
class AuthenticateComponent extends Component {
componentWillMount () {
this.checkAuth(this.props.isAuthenticated);
}
componentWillReceiveProps (nextProps) {
this.checkAuth(nextProps.isAuthenticated);
}
checkAuth (isAuthenticated) {
if (!isAuthenticated) {
let redirectAfterLogin = this.props.location.pathname;
this.props.dispatch(push(`/sign_in?next=${redirectAfterLogin}`));
}
}
render () {
return (
<div>
{this.props.isAuthenticated === true
? <Component {...this.props}/>
: null
}
</div>
)
}
}
const mapStateToProps = (state) => ({
token: state.auth.token,
isAuthenticated: state.auth.isAuthenticated
});
return connect(mapStateToProps)(AuthenticateComponent);
}
我也将 routing: routerReducer
放入我的组合减速器中。这可能是什么问题?
原来我还需要应用 routerMiddleware
我将 react-router-redux 设置为 example。但是 dispatch(push(url))
不会更改地址栏上的 content/view 或 url。尽管从我的控制台我可以看到 LOCATION_CHANGE 和 CALL_HISTORY_METHOD 已使用我给定的地址成功调用。在这种情况下,如果登录成功,它不会加载预期的 redirect
地址。
签到动作
export function loginUser(email, password, redirect="/dashboard") {
return function(dispatch) {
dispatch(loginUserRequest());
return fetch(`http://localhost:3000/users/sign_in/`, {
...
})
.then(parseJSON)
.then(response => {
try {
let decoded = jwtDecode(response.token);
dispatch(loginUserSuccess(response.token));
dispatch(push(redirect));
} catch (e) {
...
}
})
}
}
routes.js
import React from 'react';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux'
...
import store from '../store';
const history = syncHistoryWithStore(browserHistory, store)
let Routes =
<Router history={history}>
<Route path='/' component={MainContainer}>
<IndexRoute component={Home} />
<Route path='/sign_in' component={SigninForm} />
<Route path='dashboard' component={authenticateComponent(Dashboard)} />
</Route>
</Router>
export default Routes;
store.js
import { createStore, applyMiddleware, compose } from 'redux'
import thunkMiddleware from 'redux-thunk'
import createLogger from 'redux-logger'
import reducers from './reducers';
const createStoreWithMiddleware = compose(
applyMiddleware(
thunkMiddleware,
createLogger()
)
)
const store = createStore(reducers, createStoreWithMiddleware);
export default store;
AuthenticateComponent.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { push } from 'react-router-redux';
export function authenticateComponent(Component) {
class AuthenticateComponent extends Component {
componentWillMount () {
this.checkAuth(this.props.isAuthenticated);
}
componentWillReceiveProps (nextProps) {
this.checkAuth(nextProps.isAuthenticated);
}
checkAuth (isAuthenticated) {
if (!isAuthenticated) {
let redirectAfterLogin = this.props.location.pathname;
this.props.dispatch(push(`/sign_in?next=${redirectAfterLogin}`));
}
}
render () {
return (
<div>
{this.props.isAuthenticated === true
? <Component {...this.props}/>
: null
}
</div>
)
}
}
const mapStateToProps = (state) => ({
token: state.auth.token,
isAuthenticated: state.auth.isAuthenticated
});
return connect(mapStateToProps)(AuthenticateComponent);
}
我也将 routing: routerReducer
放入我的组合减速器中。这可能是什么问题?
原来我还需要应用 routerMiddleware