2019-06-27 16:22:24 +02:00
|
|
|
import React, { Component } from 'react'
|
2019-01-25 01:39:15 +01:00
|
|
|
|
|
|
|
const isServer = typeof window === 'undefined'
|
|
|
|
|
2020-06-12 00:09:06 +02:00
|
|
|
type State = JSX.Element[] | undefined
|
2019-01-25 01:39:15 +01:00
|
|
|
|
|
|
|
type SideEffectProps = {
|
2019-05-30 03:19:32 +02:00
|
|
|
reduceComponentsToState: <T>(
|
|
|
|
components: Array<React.ReactElement<any>>,
|
|
|
|
props: T
|
|
|
|
) => State
|
|
|
|
handleStateChange?: (state: State) => void
|
2020-06-12 00:09:06 +02:00
|
|
|
headManager: any
|
2019-06-27 16:22:24 +02:00
|
|
|
inAmpMode?: boolean
|
2019-01-25 01:39:15 +01:00
|
|
|
}
|
|
|
|
|
2020-06-12 00:09:06 +02:00
|
|
|
export default class extends Component<SideEffectProps> {
|
2020-06-23 22:46:40 +02:00
|
|
|
private _hasHeadManager: boolean
|
|
|
|
|
2020-06-12 00:09:06 +02:00
|
|
|
emitChange = (): void => {
|
2020-06-23 22:46:40 +02:00
|
|
|
if (this._hasHeadManager) {
|
|
|
|
this.props.headManager.updateHead(
|
|
|
|
this.props.reduceComponentsToState(
|
|
|
|
[...this.props.headManager.mountedInstances],
|
|
|
|
this.props
|
|
|
|
)
|
2020-06-12 00:09:06 +02:00
|
|
|
)
|
2020-06-23 22:46:40 +02:00
|
|
|
}
|
2019-01-25 01:39:15 +01:00
|
|
|
}
|
|
|
|
|
2020-06-12 00:09:06 +02:00
|
|
|
constructor(props: any) {
|
|
|
|
super(props)
|
2020-06-23 22:46:40 +02:00
|
|
|
this._hasHeadManager =
|
|
|
|
this.props.headManager && this.props.headManager.mountedInstances
|
|
|
|
|
|
|
|
if (isServer && this._hasHeadManager) {
|
2020-06-12 00:09:06 +02:00
|
|
|
this.props.headManager.mountedInstances.add(this)
|
|
|
|
this.emitChange()
|
2019-01-25 01:39:15 +01:00
|
|
|
}
|
2020-06-12 00:09:06 +02:00
|
|
|
}
|
|
|
|
componentDidMount() {
|
2020-06-23 22:46:40 +02:00
|
|
|
if (this._hasHeadManager) {
|
|
|
|
this.props.headManager.mountedInstances.add(this)
|
|
|
|
}
|
2020-06-12 00:09:06 +02:00
|
|
|
this.emitChange()
|
|
|
|
}
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.emitChange()
|
|
|
|
}
|
|
|
|
componentWillUnmount() {
|
2020-06-23 22:46:40 +02:00
|
|
|
if (this._hasHeadManager) {
|
|
|
|
this.props.headManager.mountedInstances.delete(this)
|
|
|
|
}
|
2020-06-12 00:09:06 +02:00
|
|
|
this.emitChange()
|
|
|
|
}
|
2019-01-25 01:39:15 +01:00
|
|
|
|
2020-06-12 00:09:06 +02:00
|
|
|
render() {
|
|
|
|
return null
|
2019-01-25 01:39:15 +01:00
|
|
|
}
|
|
|
|
}
|