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:
- Import the
useHistory
hook from React Router:
import { useHistory } from 'react-router-dom';
- Inside your component, initialize the
history
object using theuseHistory
hook:
const history = useHistory();
- To perform the redirect, use the
push
method of thehistory
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.