Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

usenavigate

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

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

useNavigate

// React Router v6
import { useNavigate } from 'react-router-dom';

export function GoHome() {
  let navigate = useNavigate();
  const handleClick = e => {
    e.preventDefault();
    navigate('/home');
  }
  return <button onClick={handleClick}>Go to Home</button>
}
Comment

how to use usenavigate in react class component

For those who are struggling with route v6 and more. You must define a function outside of your component Class and use it as following:

function myParams(Component) {
    return props => <Component navHook={useNavigate()} />;
}
in your Class component:

this.props.navHook('/SomeWhere')
And keep in mind you have wrap your Class in your function:

export default myParams(Card);
Comment

useNavigate

<Navigate to="/dashboard" replace={true} />
Comment

useNavigate in react

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

const navigate = useNavigate();

const submitHandler = async (event) => {
  event.preventDefault();

  try {
    await submitForm();
    navigate("/success"); // Omit optional second argument
  } catch (error) {
    navigate("/error", { state: { message: "Failed to submit form" } }); // Pass optional second argument
  }
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to compare two time in moment js 
Javascript :: puppeteer mac m1 
Javascript :: puppeeter mac m1 
Javascript :: regular expression for email validation 
Javascript :: how to change the color of a console.log in javascript 
Javascript :: electron preload to renderer 
Javascript :: how to click on the datepicker date in jquery 
Javascript :: authentication in strapi 
Javascript :: how to get the data from url in javascript 
Javascript :: conditional classname prop react 
Javascript :: how to get last string in javascript 
Javascript :: Check object property exists or not in js 
Javascript :: clear value input jquery 
Javascript :: jquery click on data attribute 
Javascript :: javascript print path 
Javascript :: async constructor javascript 
Javascript :: formik react native 
Javascript :: javascript hex color to rgba 
Javascript :: jquery submit form 
Javascript :: trailing zeros in factorial js 
Javascript :: remove specific element from array javascript 
Javascript :: jquery sibling 
Javascript :: get all indexes for element in array javascript 
Javascript :: flutter intl currency 
Javascript :: javascript anagram two strings 
Javascript :: base64 to blob 
Javascript :: vue js import css from node modules 
Javascript :: find from string in javascript 
Javascript :: javascript last child 
Javascript :: capitalize a string javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =