我正在使用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中调用action来存储数据
/** @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\>
该路线而无需重新加载,但是对于自动化路线,我不确定该怎么做。
任何建议将不胜感激。
对于最新版本(v2.0.0-rc5
),推荐的导航方法是直接推入历史单例。
相关代码
import { browserHistory } from './react-router'
browserHistory.push('/some/path')
如果使用更新的react-router API,则需要在组件内部使用history
from this.props
,因此:
static propTypes = {
history: React.PropTypes.object
}
this.props.history.push('/some/path');
如果使用react-router-redux
,它提供了push
可以像这样分派的功能:
import { push } from 'react-router-redux';
this.props.dispatch(push('/some/path'));
在v2.4.0及更高版本中,使用高阶组件将路由器作为组件的道具。
import { withRouter } from 'react-router'
class Example extends React.Component {
// use `this.props.router.push('/some/path')` here
};
// Export the decorated class
var DecoratedExample = withRouter(Example);
// PropTypes
Example.propTypes = {
router: React.PropTypes.shape({
push: React.PropTypes.func.isRequired
}).isRequired
};
请访问此链接以获取更多信息:http : //kechengpuzi.com/q/s31079081
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句