React-router: Question: recommended way to maintain the page title

Created on 16 Sep 2014  路  20Comments  路  Source: ReactTraining/react-router

Note: This is a question, and not an issue. Please close it if this isn't the appropriate place for it.
Question: Can you recommend a way to maintain the page title with react-router?

Most helpful comment

React helmet is great! Check it out https://github.com/nfl/react-helmet

All 20 comments

We discussed this in #107, but I don't think we ever actually figured out a "recommended" solution.

We're currently passing both state and props to a static getMetaData method that returns title and other metadata such as OpenGraph tags. This is then used in componentDidUpdate so we're sure any changes are reflected in the title at all times, this works especially well when your title might be dependent on data that might arrive in chunks.

I should put some code up on github. Here's how I handle it:

Constraints:

  1. There is an arbitrary level of nesting
  2. There is a potentially inconsistent nesting level of handlers that affect the title
  3. Asynchronous data affects the title

Method:

I make a title module that:

  1. maintains state as an array of data objects representing different segments of the title
  2. whenever handlers want to, they can push, pop, and replace data into this module
  3. the module has logic that constructs a title based on the objects in its state whenever the data changes

Its pretty much how a react component works, except it doesn't work on the dom, it sets document.title.

Since lots of people are asking about this, maybe we could just make a module like you're describing and distribute it with the router? It's not totally out of scope..

If you were going to do that, would it also make sense to allow specifying <meta> as well? Or basically anything that exists within <head />?

my module just outputs to document.title, its not actually a component or anything. It just sort of acts like one.

But ... we may want some fancy business with anything in <head/> for server-side rendering I think.

@rpflorence Definitely, you'll probably want various meta-tags for Twitterbook and Myface

SEO ALL THE THINGS!

Haven't used yet but adding for posterity's sake: react-document-title looks promising and seems like it could be a nice little addition to Route.props :+1:

Obviously, this is easy for me to say since I'm not a project maintainer :)

react-document-title is good, though it would be better to support SEO titles in react-router since react-document-title just duplicates its hierarchy. SEO title is a navigational aspect and can be lifted to router IMO.

I probably subliminally got the idea from this thread and later forgot about it.

Ivan, your point makes a lot of sense to me

I think it's best left in userland though. It's trivial to make a function to wrap your route handlers into react-document-title or whatever else comes along.

Anything new here? I'm banging my head against the wall because of this ;) (doing server-side rendering).

^I'm wondering the same thing as well. As of now I've yet to find a solution for rendering route-specific meta tags on the server.

@jaraquistain try https://github.com/gaearon/react-side-effect
It was created for such tasks

@ivan-kleshnin Thanks for the tip, I'll check it out

React helmet is great! Check it out https://github.com/nfl/react-helmet

I second react-helmet, I actually ended up using that in my implementation

Was this page helpful?
0 / 5 - 0 ratings