react history code example

Example 1: react router dom push

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

function HomeButton() {
  let history = useHistory();

  function handleClick() {
    history.push("/home");
  }

  return (
    <Button type="button" onClick={handleClick}>
      Go home
    </Button>
  );
}

Example 2: in react js how to access history in component

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

function HomeButton() {
  let history = useHistory();

  function handleClick() {
    history.push("/home");
  }

  return (
    <button type="button" onClick={handleClick}>
      Go home
    </button>
  );
}

Example 3: this.props.history.location.push

props.history.push({  pathname: '/register', state: data_you_need_to_pass});

Example 4: npm history react install command

npm i history

Example 5: use history in react router

import {createBrowserHistory} from 'history';import {useRouterHistory, Router, Route} from 'react-router';const history = useRouterHistory(createBrowserHistory)({  basename: '/basename'});history.listen(location => {  history.push('/super/url');});const router = (  <Router history={history}>    <Route path=’/' component={App}></Route>  </Router>);

Example 6: this.props.history.location.push

<Link to={{  pathname: "/register",  state: data_you_need_to_pass }}> Register</Link>