react button onClick redirect page

前端 未结 13 2235
我在风中等你
我在风中等你 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:36

    A simple click handler on the button, and setting window.location.hash will do the trick, assuming that your destination is also within the app.

    You can listen to the hashchange event on window, parse the URL you get, call this.setState(), and you have your own simple router, no library needed.

    class LoginLayout extends Component {
        constuctor() {
          this.handlePageChange = this.handlePageChange.bind(this);
          this.handleRouteChange = this.handleRouteChange.bind(this);
          this.state = { page_number: 0 }
        }
    
      handlePageChange() {
        window.location.hash = "#/my/target/url";
      }
    
      handleRouteChange(event) {
        const destination = event.newURL;
        // check the URL string, or whatever other condition, to determine
        // how to set internal state.
        if (some_condition) {
          this.setState({ page_number: 1 });
        }
      }
    
      componentDidMount() {
        window.addEventListener('hashchange', this.handleRouteChange, false);
      }
    
      render() {
        // @TODO: check this.state.page_number and render the correct page.
        return (
          <div className="app flex-row align-items-center">
            <Container>
              ...
                    <Row>
                      <Col xs="6">                      
                        <Button 
                           color="primary"
                           className="px-4"
                           onClick={this.handlePageChange}
                        >
                            Login
                         </Button>
                      </Col>
                      <Col xs="6" className="text-right">
                        <Button color="link" className="px-0">Forgot password </Button>
                      </Col>
                    </Row>
               ...
            </Container>
          </div>
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-02 15:38

    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>
    }
    
    0 讨论(0)
  • 2020-12-02 15:38

    A very simple way to do this is by the following:

    onClick={this.fun.bind(this)}
    

    and for the function:

    fun() {
      this.props.history.push("/Home");
    }
    

    finlay you need to import withRouter:

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

    and export it as:

    export default withRouter (comp_name);
    
    0 讨论(0)
  • 2020-12-02 15:38

    With React Router v5.1:

    import {useHistory} from 'react-router-dom';
    import React, {Component} from 'react';
    import {Button} from 'reactstrap';
    .....
    .....
    export class yourComponent extends Component {
        .....
        componentDidMount() { 
            let history = useHistory;
            .......
        }
    
        render() {
            return(
            .....
            .....
                <Button className="fooBarClass" onClick={() => history.back()}>Back</Button>
    
            )
        }
    }
    
    
    0 讨论(0)
  • 2020-12-02 15:39

    Don't use a button as a link. Instead, use a link styled as a button.

    <Link to="/signup" className="btn btn-primary">Sign up</Link>
    
    0 讨论(0)
  • 2020-12-02 15:39

    I was trying to find a way with Redirect but failed. Redirecting onClick is simpler than we think. Just place the following basic JavaScript within your onClick function, no monkey business:

    window.location.href="pagelink"
    
    0 讨论(0)
提交回复
热议问题