react button onClick redirect page

前端 未结 13 2237
我在风中等你
我在风中等你 2020-12-02 15:19

I am working on web application using React and bootstrap. When it comes to applying button onClick, it takes me hard time to let my page being redirect to another. if afte

相关标签:
13条回答
  • 2020-12-02 15:43

    If all above methods fails use something like this:

        import React, { Component } from 'react';
        import { Redirect } from "react-router";
        
        export default class Reedirect extends Component {
            state = {
                redirect: false
            }
            redirectHandler = () => {
                this.setState({ redirect: true })
                this.renderRedirect();
            }
            renderRedirect = () => {
                if (this.state.redirect) {
                    return <Redirect to='/' />
                }
            }
            render() {
                return (
                    <>
                        <button onClick={this.redirectHandler}>click me</button>
                        {this.renderRedirect()}
                    </>
                )
            }
        }
    
    0 讨论(0)
  • 2020-12-02 15:48

    update:

    React Router v5 with hooks:

    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;
    

    with 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;
    

    with 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);
    
    0 讨论(0)
  • 2020-12-02 15:49

    useHistory() from react-router-dom can fix your problem

    import React from 'react';
    import { useHistory } from "react-router-dom";
    function NavigationDemo() {
      const history = useHistory();
      const navigateTo = () => history.push('/componentURL');//eg.history.push('/login');
    
      return (
       <div>
       <button onClick={navigateTo} type="button" />
       </div>
      );
    }
    export default NavigationDemo;
    
    0 讨论(0)
  • 2020-12-02 15:54

    if you want to redirect to a route on a Click event.

    Just do this

    In Functional Component

    props.history.push('/link')
    

    In Class Component

    this.props.history.push('/link')
    
    

    Example:

    <button onClick={()=>{props.history.push('/link')}} >Press</button>
    

    Tested on:

    react-router-dom: 5.2.0,

    react: 16.12.0

    0 讨论(0)
  • 2020-12-02 15:56

    First, import it:

    import { useHistory } from 'react-router-dom';
    

    Then, in function or class:

    const history = useHistory();
    

    Finally, you put it in the onClick function:

    <Button onClick={()=> history.push("/mypage")}>Click me!</Button>
    
    0 讨论(0)
  • 2020-12-02 16:00

    This can be done very simply, you don't need to use a different function or library for it.

    onClick={event =>  window.location.href='/your-href'}
    
    0 讨论(0)
提交回复
热议问题