2016-10-10 65 views
0

我有一个反应应用与反应路由器的导航。我尝试创建更新某些数据的表单。提交后数据通过ajax请求发送到服务器,如果ajex响应是“好”,那么用户必须重定向到另一个页面(在我的情况下,反应路由器中的url为/carwash),并且关于成功更新的信息必须在远程页面中显示。反应重定向和传输信息到另一个页面

我试图通过3点differect的方式来做到这一点:

1)this.props.history.push('/carwash')结果是例外:Uncaught TypeError: Cannot read property 'push' of undefined

2)browserHistory.push("/carwash"),其结果是列表的浏览器被改变,但没有任何重定向

3)重定向绝对url无反应:window.location.replace("/owner.html#/carwash"),结果成功,但我不知道如何通知目标页数据更新成功。

你能否解释一下在反应中重定向的最佳方式以及如何使用它?如何在用户重定向的页面上显示数据更新成功的信息不是不那么重要?

我的代码有以下几种观点:

import React from 'react'; 
import {Router} from 'react-router'; 
import Loading from './form/loading.jsx'; 

export default class EditCarWashForm extends React.Component{ 

static contextTypes = { 
    router: React.PropTypes.object.isRequired 
}; 

constructor(props) { 
    super(props); 
    this.state = { 
     name  : {value : constants.EMPTY_FIELD, isValid : false}, 
     address  : {value : constants.EMPTY_FIELD, isValid : true}, 
     isCarWashDownload : true 
    }; 
    this.handleSubmit = this.handleSubmit.bind(this); 
} 

handleSubmit(e) { 
    e.preventDefault(); 
    let carWashData = {some logic} 

    $.ajax({ 
     url: '/carwash/', 
     type: 'POST', 
     async: true, 
     cache: false, 
     contentType: 'application/json', 
     data: JSON.stringify(carWashData), 
     dataType: 'json', 
     success: function(data) { 
      if (data.message == 'Ok'){ 
       // browserHistory.push("/owner.html#/carwash") 
       this.props.history.push('/carwash'); 
       // window.location.replace("/owner.html#/carwash"); 
     } 
     }.bind(this), 
    }) 
}; 


render(){ 
    let data = <Loading/> 

    if (this.state.isCarWashDownload) { 
     data = 
      <form onSubmit={this.handleSubmit} id="addCarWashForm"> 
       <FormInputField 
        title="Name*:" 
        placeholder="Name" 
        name="name" 
        required={true} 
        maxLength={50} 
        defaultValue={this.state.name.value} 
       /> 

       <FormInputField 
        title="Adress:" 
        placeholder="Adress" 
        name="address" 
        maxLength={200} 
       /> 


       <div className="form-group"> 
        <div className="col-sm-offset-2 col-sm-10"> 
         <button type="submit">Update</button> 
        </div> 
       </div> 

      </form> 
    } 

    return (
     <div> 
      {data} 
     </div> 
    ) 

} 

}

+0

一些概念。您创建了一个单页面应用程序(SPA),然后使用“重定向”一词,这通常不适用于SPA。你是否真的想要浏览器刷新,或者你是否需要“反应”来呈现不同的组件/页面?它们实际上是两种不同的东西。 – Gregg

+0

@Gregg对不起,但重定向它的反应的正式名称,反应路由器甚至有compinent - 设置重定向到另一个路线在您的应用程序来维护旧的URL [链接](https://github.com/ReactTraining/ react-router/blob/master/docs/API.md#重定向) –

回答

2

此代码工作重定向:

// Somewhere like a Redux middleware or Flux action: 
import { browserHistory } from 'react-router' 

// Go to /some/path. 
browserHistory.push('/some/path') 

我的错是在主要组件中负责渲染<Router>。我有下面的代码中提到的历史并不像browserHistory但hashHistory

ReactDOM.render(
<Router history={hashHistory}> 
    <Route path="/" component={MyHeader}> 
     <IndexRoute component={Main}/> 
     <Route path="carwash" component={CarWashPage} /> 
     <Route path="carwashAdd" component={AddCarWashPage} /> 
     <Route path="carwashAdd/:carWashId" component={EditCarWashPage} /> 

    </Route> 
</Router>, 
destination 
); 

鉴于该重定向工作,如果做以下修改(对我来说):你混淆

hashHistory.push('/some/path'); 
+0

这不是一个真正的重定向。但很高兴你能工作。 – Gregg

+0

@Gregg谢谢你,但它解决了我的问题的一部分。在这种重定向之后,我仍然不知道如何在目标页面中显示信息。我想显示文本,如“数据已成功更新” –

2

尝试导入browserHistory像:

// Somewhere like a Redux middleware or Flux action: 
import { browserHistory } from 'react-router' 

// Go to /some/path. 
browserHistory.push('/some/path') 

check here

+0

我做了,但没有重定向。我将以下代码添加到ajax成功中:'browserHistory.push('/ carwash')'。在控制台中,我看到ajax发送post请求,然后没有任何操作,只有浏览器中的url从'http:// localhost:8080/owner.html#/ carwashAdd/3?_k = 76k2wy'更改为'http://localhost:8080/carwash' 注意:我不使用redux或flux –

+0

使用window.location不是推荐的方法,应该避免。 有一个类似的问题,值得检查 http:// stackoverflow。com/questions/31079081 /编程式地导航使用反应路由器 –

相关问题