history get state react hooks code example

Example 1: useHistory react-router-dom

In react-router-dom version 6 
useHistory() is replaced by useNavigate() ;

import {useNavigate} from 'react-router-dom';
const navigate = useNavigate();
navigate('/home')

Example 2: uselocation hook

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

function Home() {
  const history = useHistory();
  return ;
}

Example 3: how to get state value from history react

history.push({
  pathname: '/path',
  customNameData: yourData,
});

Example 4: how to get state value from history react

history.push('/path', yourData);

Tags:

Misc Example