本文介绍了React JS:history.push 不是函数错误,它不会在点击 swal 时导航到不同的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几个月我一直在使用 react js,刚刚了解了用于 react 的 sweetalert bootstrap 插件.成功更新组件后,屏幕上会出现一个对话框.单击单击此处"按钮后,history.push 被调用,它应该路由到不同的页面,但我一直收到错误

history.push 未定义.

我在谷歌上搜索了很多东西并尝试了很多场景,但都没有成功.

代码片段:

class displayBlank extends Component {使成为(){<div id="显示详细信息"><DisplayDetails respData={this.state.respData}/>

}}导出默认显示空白;类显示详细信息{句柄更新(){//HandleUpdate 主体swal({title :"客户更新成功!",图标:成功",按钮:点击这里!",}).then((e) => { history.push('/blank');});}使成为(){<表格><tr><td>表格网格内容</td></tr><td><FormGroup controlId="formControlsDisabledButton" style={buttonalignment}><Button className="float-right" bsStyle="primary" type="submit" onClick={this.handleUpdate}>保存</Button>{' '}<Button className="float-right" bsStyle="primary" type="submit" disabled>下载</Button>{' '}</FormGroup></td>}}

解决方案

如果你正在使用 react-router,使用this.props.history.push('').

如果您遇到 Cannot read property 'push' of undefined 错误,请将组件包装在 withRouter:

import { withRouter } from 'react-router';类 DisplayBlank 扩展组件 {...}导出默认 withRouter(DisplayBank);

I have been using react js for the past couple of months and just got to know about the sweetalert bootstrap plugin for react. on update of a component successfully a dialog appears onscreen. upon clicking the "click here" button history.push is invoked and it should route to a different page but I have been getting error

I have googled quite a few stuff and tried quite a few scenarios but haven't been successful in it.

code snippet:

class displayBlank extends Component {

render(){

<div id="displayDetails">
          <DisplayDetails respData={this.state.respData}  />
        </div>

}

}

export default displayBlank;


class displayDetails{

 handleUpdate()
 {

  // Body of HandleUpdate
    swal({
      title :"Customer Updated Successfully!",
      icon : "success",
      button: "Click Here!",
    }).then((e) => { history.push('/blank');
  });

  }

  render(){

  <table>
  <tr>
  <td>

   Table-Grid Contents
  </td>
  </tr>
  </table>

  <td>
              <FormGroup controlId="formControlsDisabledButton"  style={buttonalignment}>
                <Button className="float-right" bsStyle="primary" type="submit" onClick={this.handleUpdate}>Save</Button>
                {'  '}
                <Button className="float-right" bsStyle="primary" type="submit" disabled>Download</Button>
                {'  '}
              </FormGroup>
            </td>


  }

}  
解决方案

If you're using react-router, use this.props.history.push('<url>').

If you're stumbling across a Cannot read property 'push' of undefined error, wrap the component in withRouter:

import { withRouter } from 'react-router';

class DisplayBlank extends Component {
  ...
}

export default withRouter(DisplayBank);

这篇关于React JS:history.push 不是函数错误,它不会在点击 swal 时导航到不同的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 04:39