首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >React JS : history.push不是一个函数错误,它不会通过单击swal导航到不同的页面

React JS : history.push不是一个函数错误,它不会通过单击swal导航到不同的页面
EN

Stack Overflow用户
提问于 2018-12-11 04:06:05
回答 3查看 27.9K关注 0票数 10

在过去的几个月里,我一直在使用react js,我刚刚了解了react的甜蜜警示引导插件。成功更新组件时,屏幕上会出现一个对话框。单击"click here“按钮后,history.push会被调用,它应该会路由到不同的页面,但我一直收到错误

未定义

history.push。

我在谷歌上搜索了相当多的东西,尝试了相当多的场景,但都没有成功。

代码片段:

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>


  }

}  
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-11 05:37:44

如果您使用的是react-router,请使用this.props.history.push('<url>')

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

import { withRouter } from 'react-router';

class DisplayBlank extends Component {
  ...
}

export default withRouter(DisplayBank);
票数 24
EN

Stack Overflow用户

发布于 2021-12-07 05:48:15

您好,如果您面临的问题是

react js中的

history.push试试这个

 import { useNavigate } from "react-router-dom";
        const history= useNavigate();
   
   history("/path");
票数 4
EN

Stack Overflow用户

发布于 2021-03-22 02:31:58

您只需在函数参数中将propshistory作为对象传递即可。

function =({history})=>{
   return <div>Hello</div>;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53712860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档