Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update with-react-intl example #4825

Merged
merged 2 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/with-react-intl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ This example app shows how to integrate [React Intl][] with Next.
- `<IntlProvider>` creation with `locale`, `messages`, and `initialNow` props
- Default message extraction via `babel-plugin-react-intl` integration
- Translation management via build script and customized Next server
- withIntl HOC for pages because injectIntl do not hoist static methods.

### Translation Management

Expand Down
11 changes: 11 additions & 0 deletions examples/with-react-intl/lib/withIntl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import hoistNonReactStatics from 'hoist-non-react-statics'
import { injectIntl } from 'react-intl'

export const hoistStatics = (higherOrderComponent) => (BaseComponent) => {
const NewComponent = higherOrderComponent(BaseComponent)
hoistNonReactStatics(NewComponent, BaseComponent)

return NewComponent
}

export default hoistStatics(injectIntl)
1 change: 1 addition & 0 deletions examples/with-react-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"accepts": "^1.3.3",
"babel-plugin-react-intl": "^2.3.1",
"glob": "^7.1.1",
"hoist-non-react-statics": "^3.0.0-rc.1",
"intl": "^1.2.5",
"next": "latest",
"react": "^16.0.0",
Expand Down
1 change: 1 addition & 0 deletions examples/with-react-intl/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class MyApp extends App {
render () {
const { Component, pageProps, locale, messages } = this.props
const now = Date.now()

return (
<Container>
<IntlProvider locale={locale} messages={messages} initialNow={now}>
Expand Down
45 changes: 29 additions & 16 deletions examples/with-react-intl/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import React from 'react'
import {FormattedMessage, FormattedNumber, defineMessages, injectIntl} from 'react-intl'
import React, { Component } from 'react'
import { FormattedMessage, FormattedNumber, defineMessages } from 'react-intl'
import Head from 'next/head'
import Layout from '../components/Layout'
import withIntl from '../lib/withIntl'

const {description} = defineMessages({
const { description } = defineMessages({
description: {
id: 'description',
defaultMessage: 'An example app integrating React Intl with Next.js'
}
})

export default injectIntl(({intl}) => (
<Layout>
<Head>
<meta name='description' content={intl.formatMessage(description)} />
</Head>
<p>
<FormattedMessage id='greeting' defaultMessage='Hello, World!' />
</p>
<p>
<FormattedNumber value={1000} />
</p>
</Layout>
))
class Index extends Component {
static getInitialProps () {
// Do something
}

render () {
const { intl } = this.props

return (
<Layout>
<Head>
<meta name='description' content={intl.formatMessage(description)} />
</Head>
<p>
<FormattedMessage id='greeting' defaultMessage='Hello, World!' />
</p>
<p>
<FormattedNumber value={1000} />
</p>
</Layout>
)
}
}

export default withIntl(Index)