I've found this solution for redirecting to an external link, and it has worked for one of my pages.
But for a different page it is no longer working, and I'm not sure why.
redirect.jsx
:
import React from "react";
import { BrowserRouter, Route } from 'react-router-dom'
export default class Redirect extends React.Component {
render() {
return (
<Route path='/hello' component={() => {
window.location.href = "example.com";
return null;}}/>
);
}
}
test.jsx
:
import React from "react";
import ReactDOM from "react-dom";
import {BrowserRouter} from 'react-router-dom'
import MainExport from './components/redirect.jsx';
ReactDOM.render(
<BrowserRouter>
<div>
<MainExport />
</div>
</BrowserRouter>, document.getElementById("content")
);
test.html
:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div className="App">
<div id="content" />
<div/>
<script src="dist/bundle/test.bundle.js" type="text/javascript"></script>
</body>
</html>
When I load this page, I expect it to redirect to the URL "/hello" and display the page from "example.com".
Instead, it does nothing.
What am I doing wrong?
EDIT: Some of the answers have been helpful, but none of them have worked yet.
I've found that using the Redirect
component renders and does the redirect, but does it in some way such that my Flask server doesn't actually serve the file. All I see is a blank screen.
Changing the name to MyRedirect
also did not make it work.
Using render
instead of component
also did not seem to have an impact.
The redirect is still not taking place.
(I'm also still not seeing any error messages in the JS console or on the server side.)