rsnext/examples/with-lingui/components/withLang.js
Matthew Webb 9ccbb1f5bf Correctly pass props down in with-lingui example HOC (#7445)
* correctly pass props down in Lingui example HOC

The WithLang HOC provided as part of the with-lingui example does not pass props down to the wrapped component.
This means that the result of getInitialProps is not passed to the page component.
This commit passes all props which aren't specific to the HOC implementation down to the wrapped component.

* remove semicolon to satisfy lint rules
2019-05-29 10:43:47 +02:00

34 lines
859 B
JavaScript

import React from 'react'
import { I18nProvider } from '@lingui/react'
export default (Component, defaultLang = 'en') =>
class WithLang extends React.Component {
static async getInitialProps (ctx) {
const language = ctx.query.lang || defaultLang
const [props, catalog] = await Promise.all([
Component.getInitialProps ? Component.getInitialProps(ctx) : {},
import(`../locale/${language}/messages.po`).then(m => m.default)
])
return {
...props,
language,
catalogs: {
[language]: catalog
}
}
}
render () {
const { language, catalogs, ...restProps } = this.props
return (
<I18nProvider
language={language}
catalogs={catalogs}
>
<Component {...restProps} />
</I18nProvider>
)
}
}