我正在使用 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> ); } }
更新:
React Router v6:
import React from 'react'; import { useNavigate } from "react-router-dom"; function LoginLayout() { let navigate = useNavigate(); const routeChange = () =>{ let path = `newPath`; navigate(path); } return ( <div className="app flex-row align-items-center"> <Container> ... <Button color="primary" className="px-4" onClick={routeChange} > Login </Button> ... </Container> </div> ); }}
带有钩子的 React Router 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);