I have the following code that redirects automatically to the About component when you load the FAQ component. This is done using a custom hook that uses useHistory() to push a new route to it. The custom hook also returns an empty object. When you don't try to use anything from this object, the code works perfectly and the app redirects to the About component.
But when I try to access a (non-existing) value from the object, I get the value is null error. I don't understand why the code even reaches this. Shouldn't the redirect happen before the rest of the component is rendered?
import React from "react";
import "./styles.css";
import { Link, BrowserRouter, Route, useHistory } from "react-router-dom";
export default function App() {
return (
<div className="App">
<BrowserRouter>
<Link to="/about">About</Link>
<Link to="/faq">FAQ</Link>
<Route path="/about" component={About} />
<Route path="/faq" component={FAQ} />
</BrowserRouter>
</div>
);
}
function useRedirect() {
const history = useHistory();
history.push("/about");
return {};
}
function FAQ() {
const { value } = useRedirect();
return <h1>FAQ {value.name}</h1>;
}
function About() {
return <h1>About</h1>;
}
Why does the redirect not happen before the value is accessed? And how can I make sure that my app redirects before accessing the value?