import React from 'react'; interface IWatchValueProps { value: T; onChange: (newValue: T, oldValue: T) => void; isEqual?: (newValue: T, oldValue: T) => boolean; } interface IWatchValueState { value: T; } export class WatchValue extends React.Component, IWatchValueState> { public static defaultProps: Partial> = { isEqual: (newValue: any, oldValue: any) => newValue === oldValue, }; constructor(props: IWatchValueProps) { super(props); const { value } = props; this.state = { value }; } public componentDidUpdate(): void { if (!this.props.onChange) { return; } const { value, isEqual } = this.props; const { value: prevValue } = this.state; if (!isEqual(value, prevValue)) { this.setState({ value }); this.props.onChange(value, prevValue); } } public render() { return this.props.children || null; } }