@ryan.murray
In React.js, you can redirect to another page using the react-router-dom
package. Here's how you can do it:
1
|
npm install react-router-dom |
1
|
import { BrowserRouter as Router, Route, Redirect } from 'react-router-dom'; |
1 2 3 4 5 6 7 8 |
import { BrowserRouter as Router } from 'react-router-dom'; ReactDOM.render( <Router> <App /> </Router>, document.getElementById('root') ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import { Redirect } from 'react-router-dom'; function YourComponent() { const shouldRedirect = true; // condition to check if redirect should happen if (shouldRedirect) { // Redirect to the desired page return <Redirect to="/your-target-page" />; } // Render your component as usual return ( <div> // Your component's content </div> ); } |
Replace /your-target-page
with the actual URL to which you want to redirect.
By conditionally rendering the Redirect
component, you can control when the redirection occurs.
Note: Make sure to set up and configure your routes in the Router
component appropriately.