I'm looking for a way to modify the page title when React-Router v4+ changes locations. I used to listen for a location change action in Redux and check that route against a metaData
object.
When using React-Router v4+, there's no fixed routes list. In fact, various components around the site could use Route
with the same path string. That means old method I used won't work anymore.
Is there a way I can update the page title by calling actions when certain major routes are changed or is there a better a better method to update the site's metadata?
I asked question "how to set title correctly" on official github repo of "react router dom". They answered "This isn't something the Router does. You're looking for a library like react-helmet.".
If you want to change title in react router tutorial, you just need to
npm install react-helmet
after that go to your react router dom page (eg contact.jsx) and add such code
import { Helmet } from "react-helmet";
export default function Contact() {
const { contact } = useLoaderData();
return (
<div id="contact">
<Helmet>
<title>{ contact.first??'Новый контакт' }</title>
</Helmet>
And it works correctly in march 2023.