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 after a href , I cannot go the another page.
So would you please tell me is there any need for using react-navigation or other to navigate the page using 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> ); } }
To redirect to another page on button click in React: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function, passing it the path - navigate('/about') . The navigate() function lets us navigate programmatically.
To navigate to the courses route, we will use the history. push method of the useHistory object. We will add an event handler “onClick” for our button component and define a function “coursesPage ” that handles the click event. The coursesPage function enables us to redirect to another route on clicking the button.
To redirect page on click of a button with React Router v6, we use the useNavigate hook. to call the useNavigate hook to return the navigate function. Then we call navigate with path to navigate to the path in the routeChange function.
update:
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 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With