react按钮点击重定向页面

IT技术 javascript reactjs bootstrap-4
2021-04-06 16:26:25

我正在使用 React 和 bootstrap 开发 Web 应用程序。在应用 onClick 按钮时,我很难让我的页面重定向到另一个页面。如果在 href 之后,我无法转到另一页。

那么请您告诉我是否需要使用 react-navigation 或其他方式使用 Button onClick 导航页面?

import React, { Component } from 'react';
import { Button, Card, CardBody, CardGroup, Col, Container, Input, InputGroup, InputGroupAddon, InputGroupText, Row, NavLink  } from 'reactstrap';

class LoginLayout extends Component {

  render() {
    return (
 <div className="app flex-row align-items-center">
        <Container>
     ...
                    <Row>
                      <Col xs="6">                      
                        <Button color="primary" className="px-4">
                            Login
                         </Button>
                      </Col>
                      <Col xs="6" className="text-right">
                        <Button color="link" className="px-0">Forgot password?</Button>
                      </Col>
                    </Row>
               ...
        </Container>
      </div>
    );
  }
}
6个回答

更新

使用钩子响应路由器 v5:

import React from 'react';
import { useHistory } from "react-router-dom";
function LoginLayout() {

  const history = useHistory();

  const routeChange = () =>{ 
    let path = `newPath`; 
    history.push(path);
  }

  return (
      <div className="app flex-row align-items-center">
        <Container>
          ...
          <Row>
            <Col xs="6">                      
              <Button color="primary" className="px-4"
                onClick={routeChange}
                  >
                  Login
                </Button>
            </Col>
            <Col xs="6" className="text-right">
              <Button color="link" className="px-0">Forgot password?</Button>
            </Col>
          </Row>
          ...
        </Container>
      </div>
  );
}
export default LoginLayout;

使用 React Router v5:

import { useHistory } from 'react-router-dom';
import { Button, Card, CardBody, CardGroup, Col, Container, Input, InputGroup, InputGroupAddon, InputGroupText, Row, NavLink  } from 'reactstrap';

class LoginLayout extends Component {

  routeChange=()=> {
    let path = `newPath`;
    let history = useHistory();
    history.push(path);
  }

  render() {
    return (
      <div className="app flex-row align-items-center">
        <Container>
          ...
          <Row>
            <Col xs="6">                      
              <Button color="primary" className="px-4"
                onClick={this.routeChange}
                  >
                  Login
                </Button>
            </Col>
            <Col xs="6" className="text-right">
              <Button color="link" className="px-0">Forgot password?</Button>
            </Col>
          </Row>
          ...
        </Container>
      </div>
    );
  }
}

export default LoginLayout;

使用 React Router v4:

import { withRouter } from 'react-router-dom';
import { Button, Card, CardBody, CardGroup, Col, Container, Input, InputGroup, InputGroupAddon, InputGroupText, Row, NavLink  } from 'reactstrap';

class LoginLayout extends Component {
  constuctor() {
    this.routeChange = this.routeChange.bind(this);
  }

  routeChange() {
    let path = `newPath`;
    this.props.history.push(path);
  }

  render() {
    return (
      <div className="app flex-row align-items-center">
        <Container>
          ...
          <Row>
            <Col xs="6">                      
              <Button color="primary" className="px-4"
                onClick={this.routeChange}
                  >
                  Login
                </Button>
            </Col>
            <Col xs="6" className="text-right">
              <Button color="link" className="px-0">Forgot password?</Button>
            </Col>
          </Row>
          ...
        </Container>
      </div>
    );
  }
}

export default withRouter(LoginLayout);
我想没有.. 你用 包裹你的组件withRouter吗?
2021-05-23 16:26:25
使用上面的代码后,我仍然在同一页面上。除了这个,我还需要做任何额外的工作吗?
2021-06-03 16:26:25
你能把它添加到沙箱中并给我提供链接以便我更好地解决你的问题吗
2021-06-04 16:26:25
在 v5.1.2 中不能用钩子做到这一点。React Hook "useHistory" is called in function "routeChange" which is neither a React function component or a custom React Hook function.编辑:const history = useHistory()如果您遇到此问题,请移至处理程序之外。
2021-06-04 16:26:25
我已经用withRouter. 如果我使用单个资源,它就会完美地工作,例如它适用于路线/MyProfile但是当我使用/MyProfile/edit. 如何重定向到嵌套资源?
2021-06-07 16:26:25

不要使用按钮作为链接。相反,使用样式为按钮的链接。

<Link to="/signup" className="btn btn-primary">Sign up</Link>
但这不是问题。
2021-05-23 16:26:25
我收到一个错误,不确定是否与此有关, link is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`.
2021-06-06 16:26:25

react-router v5.1.2:

import { useHistory } from 'react-router-dom';
const App = () => {
   const history = useHistory()
   <i className="icon list arrow left"
      onClick={() => {
        history.goBack()
   }}></i>
}
由于“类型错误:无法读取未定义的属性“历史”,我已经在“const history = useHistory()”这一行失败了。您是否在 index.js 中使用了 react-router-dom 的任何内容?
2021-05-25 16:26:25

这可以非常简单地完成,您不需要为它使用不同的函数或库。

onClick={event =>  window.location.href='/your-href'}
@JamesPoulose 正是!在不重新加载整个页面的情况下推荐的方法是什么?我在谈论路由 onClick。例如,当我单击详细信息图标时。提前致谢
2021-05-28 16:26:25
如果与功能组件的 useHistory 挂钩相比,这有点慢。重新加载可能是原因
2021-05-30 16:26:25
注意:这将重新加载页面。
2021-06-15 16:26:25

我试图找到重定向的方法但失败了。重定向 onClick 比我们想象的要简单。只需将以下基本 JavaScript 放在您的 onClick 函数中,不要乱搞:

window.location.href="pagelink"
是的,这违背了使用 react 制作单页应用程序的目的,会使您的所有 react 代码重新发送等(诸如历史之类的东西可能会被重置)
2021-05-27 16:26:25
注意:这将重新加载页面。
2021-05-29 16:26:25