如何在不重新加载整个页面的情况下进行路由?

Sijan Shrestha

我正在使用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\>该路线而无需重新加载,但是对于自动化路线,我不确定该怎么做。

任何建议将不胜感激。

Shubham Khatri

对于最新版本(v2.0.0-rc5),推荐的导航方法是直接推入历史单例。

演示

相关代码

import { browserHistory } from './react-router'
browserHistory.push('/some/path')

如果使用更新的react-router API,则需要在组件内部使用historyfrom 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在不重新加载页面的情况下对数据进行分页?

Django / Js:如何在不重新加载整个页面的情况下发布表单

如何在不重新加载页面的情况下更改页面网址

如何在不重新加载页面的情况下获得执行 PHP 函数的按钮?

在Ajax中成功后如何在不重新加载页面的情况下更改url

如何在不重新加载页面的情况下修改URL?

如何在不重新加载页面的情况下“刷新”Angular 中的组件?

如何在不重新加载页面的情况下获取表单数据?

如何在不重新加载页面的情况下刷新angular 8中的元素?

如何在不重新加载页面的情况下向我的网址添加参数?

如何在不重新加载页面的情况下使用ajax发送数据?(节点)

如何在不重新加载页面的情况下刷新Selenium Webdriver DOM数据?

如何在不重新加载页面的情况下使用 UWP NavigationView 后退按钮

如何在不重新加载页面的情况下重置Rhinoslider Jquery插件?

如何在不重新加载页面的情况下使用javascript过滤html表?

如何在不重新加载页面的情况下更改php include?

如何在不重新加载页面的情况下提交文件选择表单?

如何在不重新加载页面的情况下更改URL?-扭曲

如何在不重新加载页面的情况下刷新div和mysql代码

如何在不重新加载页面的情况下更新 localStorage?

如何在不刷新整个页面的情况下重新加载组件?

无法在不重新加载整个页面的情况下用Enter键提交编号

我有三个div,如何在不重新加载整个页面的情况下刷新第二个div?

如何在不重新加载页面的情况下从Javafx编辑HTML页面(在Web视图中)?

如何在不重新加载页面的情况下使用php页面上的按钮执行功能?

如何提交表单如何在不重新加载表单提交页面的情况下获取会话计数

如何在Nuxt SSR模式下更改页面的URL,而不重新加载整个页面?

如何在不重新加载页面的情况下检查表单中的确认密码字段

如何在不重新加载页面的情况下从文本字段值执行查找并填充表单元素?