xxxxxxxxxx
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
}
};
xxxxxxxxxx
// 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>
}
xxxxxxxxxx
import React, { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
const LoginRedirect = ({ loginCount, role }) => {
const navigate = useNavigate();
useEffect(() => {
// Check login count and role here
if (loginCount === 1) {
if (role === 'customer' || role === 'technician') {
// Redirect to the appropriate form
if (role === 'customer') {
navigate('/customer-form');
} else {
navigate('/technician-form');
}
}
} else {
// Redirect to the dashboard
navigate('/dashboard');
}
}, [navigate, loginCount, role]);
// Placeholder return (not rendered in this component)
return null;
};
export default LoginRedirect;