Commit b05df708 authored by Fredrik Höglund's avatar Fredrik Höglund Committed by Tim Neutkens

Fix first render of with-react-helmet example (#6235)

parent 63c25a9c
...@@ -46,4 +46,5 @@ now ...@@ -46,4 +46,5 @@ now
This an minimalistic example of how to combine next.js and [react-helmet](https://github.com/nfl/react-helmet). This an minimalistic example of how to combine next.js and [react-helmet](https://github.com/nfl/react-helmet).
The title of the page shall be changed to "Hello next.js!" The title of the page shall be changed to "Hello next.js!"
If you go to the page `/about`, the title will be overridden to "About | Hello next.js!"
The rest is all up to you. The rest is all up to you.
import React from 'react'
import App, { Container } from 'next/app'
import Helmet from 'react-helmet'
export default class MyApp extends App {
static async getInitialProps ({ Component, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return { pageProps }
}
render () {
const { Component, pageProps } = this.props
return (
<Container>
<Helmet
htmlAttributes={{ lang: 'en' }}
title='Hello next.js!'
meta={[
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ property: 'og:title', content: 'Hello next.js!' }
]}
/>
<Component {...pageProps} />
</Container>
)
}
}
...@@ -26,24 +26,10 @@ export default class extends Document { ...@@ -26,24 +26,10 @@ export default class extends Document {
.map(el => this.props.helmet[el].toComponent()) .map(el => this.props.helmet[el].toComponent())
} }
get helmetJsx () {
return (
<Helmet
htmlAttributes={{ lang: 'en' }}
title='Hello next.js!'
meta={[
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ property: 'og:title', content: 'Hello next.js!' }
]}
/>
)
}
render () { render () {
return ( return (
<html {...this.helmetHtmlAttrComponents}> <html {...this.helmetHtmlAttrComponents}>
<Head> <Head>
{this.helmetJsx}
{this.helmetHeadComponents} {this.helmetHeadComponents}
</Head> </Head>
<body {...this.helmetBodyAttrComponents}> <body {...this.helmetBodyAttrComponents}>
......
import React from 'react' import React from 'react'
import Helmet from 'react-helmet' import Helmet from 'react-helmet'
export default class About extends React.Component { export default function About () {
static async getInitialProps ({ req }) { return (
if (req) { <div>
Helmet.renderStatic() <Helmet
} title='About | Hello next.js!'
return { title: 'About' } meta={[{ property: 'og:title', content: 'About' }]}
} />
About the World
render () { </div>
const { title } = this.props )
return (
<div>
<Helmet
title={`${title} | Hello next.js!`}
meta={[{ property: 'og:title', content: title }]}
/>
About the World
</div>
)
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment