我正在使用react-route,并且在路由方面遇到了一些问题。
整个页面重新加载,导致我已经获取并存储在reducer中的所有数据每次都要重新加载。
这是我的路由文件:
var CustomRoute = React.createClass({
render:function(){
return <Router history={browserHistory}>
<Route path="/" component={Layout}>
<IndexRedirect to="/landing" />
<Route path="landing" component={Landing} />
<Route path="login" component={Login} />
<Route path="form" component={Form} />
<Route path="*" component={NoMatch} />
</Route>
</Router>
},
});
在路由之前,我已经通过调用main.jsx中的操作存储了数据。
/** @jsx React.DOM */
'use strict'
var ReactDOM = require('react-dom')
var React = require('react')
var Index = require('./components/index')
var createStore = require("redux").createStore
var applyMiddleware = require("redux").applyMiddleware
var Provider = require("react-redux").Provider
var thunkMiddleware = require("redux-thunk").default
var reducer = require("./reducers")
var injectTapEventPlugin =require('react-tap-event-plugin');
injectTapEventPlugin()
var createStoreWithMiddleware=applyMiddleware(thunkMiddleware)(createStore);
var store=createStoreWithMiddleware(reducer);
store.dispatch(actions.load_contacts()) //////////// <<<<<< this is what i call to store data already
ReactDOM.render( <Provider store={store}><Index/></Provider> , document.getElementById('content'))
问题是,如果成功登录,我必须路由到另一个页面:
this.props.dispatch(actions.login(this.state.login,this.state.password,(err)=>{
this.setState({err:err})
if (!err){
window.location = "/form"
}
}));
window.location可以实现路由跳转,但会重新加载所有内容,效率较低。对于手动路由,我使用<Link\>
来在不重新加载页面的情况下进行路由跳转,但对于自动化路由,我不确定该如何处理。
非常感谢任何建议。
var browserHistory = require('react-router').browserHistory
, 并调用browserHistory.push('/form')
即可。 - Sijan Shrestha