Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

equivalent of useHistory in react

import { useHistory } from "react-router-dom"; // DEPRECATED
// It is now
import { useNavigate } from "react-router-dom"; // As at March 3, 2022
Comment

usehistory example

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

function Home() {
  const history = useHistory();
  return <button onClick={() => history.push('/profile')}>Profile</button>;
}
Comment

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')
Comment

useHistory hook

// useHistory() does not work inside an arrow function 
// notice @ line 9 that the history.push() is inside a usual function. not an arrow function

let myComponent = () => {
    
const history = useHistory();
  function routeChange(){
   history.push("/author");
  }
  
  return(<>
  <button onClick={ routeChange} > redirect </button>
  </>)
    
}

      
      
      
      
Comment

useHistory() hook

// useHistory() has been changed in v6, so instead use useNavigate()

check out the source link
Comment

useHistory

replaced by useNavigate
Comment

PREVIOUS NEXT
Code Example
Javascript :: math.max 
Javascript :: capitalize first letter in array of strings javascript 
Javascript :: closest js 
Javascript :: react protected route 
Javascript :: jQuery DataTables Checkboxes 
Javascript :: array -1 javascript 
Javascript :: console.log() Print a Sentence 
Javascript :: jquery get value of element 
Javascript :: context api 
Javascript :: Turn on modern JS by adding use strict to your script 
Javascript :: parsedate javascript 
Javascript :: how to convert div to image in jquery 
Javascript :: javascript date array 
Javascript :: datatables keep order and page selection page refresh 
Javascript :: findone and update mongoose 
Javascript :: how to make an event listener only work once 
Javascript :: jquery onclick multiple buttons 
Javascript :: clone aJavaScript object 
Javascript :: promise all 
Javascript :: create multidimensional array javascript for loop 
Javascript :: mongodb insertmany 
Javascript :: find how many similar object item in an array in javascript 
Javascript :: jquery bootstrap checkbox val 
Javascript :: how to append in javascript 
Javascript :: react chartjs 2 
Javascript :: console.log(...) is not a function 
Javascript :: nodejs express flash message 
Javascript :: convert string to int javascript 
Javascript :: set date to input date 
Javascript :: copia array javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =