Skip to content

Instantly share code, notes, and snippets.

@danielkcz
Created August 23, 2018 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkcz/e334c937cfe3df507b73f041fa9c490e to your computer and use it in GitHub Desktop.
Save danielkcz/e334c937cfe3df507b73f041fa9c490e to your computer and use it in GitHub Desktop.
import { Trans } from '@lingui/react'
import React, { Children } from 'react'
interface IProps {
children: ReactNode | string
}
interface IState {
refs: React.RefObject<Trans>[]
}
export class PageTitle extends React.Component<IProps, IState> {
state: IState = { refs: [] }
static getDerivedStateFromProps(nextProps: IProps) {
return {
refs: Children.map(nextProps.children, child => React.createRef<Trans>()),
}
}
updateTitle() {
// @ts-ignore side effect
document.title = this.state.refs
// @ts-ignore
.map(ref => ref.current.getWrappedInstance().getTranslation())
.join(' - ')
}
componentDidMount() {
this.updateTitle()
}
componentDidUpdate() {
this.updateTitle()
}
render() {
const { children, ...props } = this.props
return (
<>
{typeof children !== 'string' &&
React.Children.map(children, (child, i) =>
React.cloneElement((child as ReactNode)!, {
ref: this.state.refs[i],
}),
)}
<h1 {...props}>{children}</h1>
</>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment