concept title in category isomorphic web applications

This is an excerpt from Manning's book Isomorphic Web Applications: Universal Development with React.
Listing 3.8. Test the listItem done prop—components/stories/chapter_3_4.js
storiesOf('Chapter 3.4 examples', module) .add('JSX basic button', () => ()) .add('link displays with props', () => ()) .add('list item functional', () => ()) .add('list item with done prop', () => ( #1 <ListItemFunctional completeCallback={actions.complete} deleteCallback={actions.delete} name="Cleanup mess" done="true"/> #2 )) .add('list container with list item', () =>()); #1 Add a story with the add() function—pass in a title for the story and a function that returns the component you want to render. #2 Add done property—component checks for truthy value here, so you can set it to true. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":4,\"ch\":2},{\"line\":4,\"ch\":70}],[{\"line\":5,\"ch\":4},{\"line\":5,\"ch\":59}],[{\"line\":6,\"ch\":24},{\"line\":6,\"ch\":55}],[{\"line\":7,\"ch\":24},{\"line\":7,\"ch\":43}],[{\"line\":8,\"ch\":24},{\"line\":8,\"ch\":70}],[{\"line\":1,\"ch\":34},{\"line\":1,\"ch\":36}],[{\"line\":2,\"ch\":42},{\"line\":2,\"ch\":44}],[{\"line\":3,\"ch\":38},{\"line\":3,\"ch\":40}],[{\"line\":9,\"ch\":2},{\"line\":9,\"ch\":4}],[{\"line\":10,\"ch\":46},{\"line\":10,\"ch\":48}]]"} !@%STYLE%@!
Titles are important for SEO and for a good user experience. The combination of title and favicon makes your tab or window in a browser distinct from all the others. Figure 10.5 shows what this looks like in the browser.
Handling the title on the server is similar to handling the metadata on the server. Each top-level component should provide a static function that outputs the title. In addition, I suggest breaking out the title output from the function that takes in the server data so you can reuse some of your logic on the browser. Add the code in the following listing to the Detail component.
Listing 10.12. Create title static function—src/components/detail.jsx
class Detail extends React.Component { static createTitle(props) { #1 return `${props.name} - All Things Westies`; } static getTitle(params, store) { #2 const currentProduct = store.products && store.products.currentProduct; return Detail.createTitle(currentProduct); } static createMetatags(params, store) {} //...other code } #1 Static function creates the title from props, an abstraction that’ll make creating the title on the browser easier. #2 On the server, renderView middleware will call getTitle, which takes in the store and pulls out the currentProduct data. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":2},{\"line\":2,\"ch\":77}],[{\"line\":3,\"ch\":4},{\"line\":3,\"ch\":48}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":25}],[{\"line\":4,\"ch\":2},{\"line\":4,\"ch\":3}],[{\"line\":10,\"ch\":2},{\"line\":10,\"ch\":3}],[{\"line\":12,\"ch\":40},{\"line\":12,\"ch\":41}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":1}],[{\"line\":6,\"ch\":2},{\"line\":6,\"ch\":77}],[{\"line\":7,\"ch\":4},{\"line\":7,\"ch\":75}],[{\"line\":9,\"ch\":4},{\"line\":9,\"ch\":46}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":25}],[{\"line\":4,\"ch\":2},{\"line\":4,\"ch\":3}],[{\"line\":10,\"ch\":2},{\"line\":10,\"ch\":3}],[{\"line\":12,\"ch\":40},{\"line\":12,\"ch\":41}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":1}]]"} !@%STYLE%@!After you have a component with the proper static function, you can set up the title on the server. In the renderView code, you add a call to get the title. The following listing shows the code you add to renderView.jsx.
Listing 10.13. Add title for the route—src/middleware/renderView.jsx
const title = flattenStaticFunction( renderProps, 'getTitle', serverState ); const app = renderToString(); const html = renderToString( <HTML html={app} serverState={stringifiedServerState} metatags={seoTags} title={title} /> );The first lines call flattenStaticFunction with the React Router renderProps, the name of the function (getTitle), and the serverState so that the title details can be fetched. A string title is returned. At the end, you pass the title into the HTML component.
Finally, you need to change the title in html.jsx to use the passed-in prop. The following listing shows you what to add.
Listing 10.14. Render the title for the route—src/components/html.jsx
<head> <title dangerouslySetInnerHTML={{ __html: props.title || 'All Things Westies' }} /> {metatagsArray} // ...more code </head>Use dangerouslySetInnerHTML to output the title string. If you render the title directly into the tag, React will append a comment that will then show up in the browser. If you run the server and reload the detail page, the title will be based on the product title.
Next, let’s look at updating the title in the browser.