how to redirect in ionic react

Redirecting in Ionic React

To redirect in Ionic React, you can use the useHistory hook from React Router. Here's how you can do it:

  1. Import the useHistory hook from React Router:
import { useHistory } from 'react-router-dom';
  1. Inside your component, initialize the history object using the useHistory hook:
const history = useHistory();
  1. To perform the redirect, use the push method of the history object and pass the desired route path as an argument:
history.push('/path-to-redirect');

Here's an example of how the code would look like in a component:

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

const MyComponent = () => {
  const history = useHistory();

  const handleRedirect = () => {
    history.push('/path-to-redirect');
  };

  return (
    <button onClick={handleRedirect}>Redirect</button>
  );
};

Please note that the useHistory hook is part of React Router, which is commonly used in Ionic React applications for navigation and routing. Make sure you have React Router installed in your project before using this hook.

[4]