import React, { useState } from 'react';
interface Props {
text: string;
}
const MyComponent = (props: Props) => {
const [currentText, setText] = useState<string>(step.description);
const hasChanged = useHasChanged([
// hasChanged will be true if either of these values
// change when compared to each other.
[ currentText, props.text ]
]);
return (
<div>
<input type="text" onInput={(evt) => setText(evt.currentTarget.value) } />
<button disabled={!hasChanged}>Save<button>
</div>
);
};
Specify a custom comparator function as a third element in the array pair to do complex comparisons:
interface ComplexType {
value: string;
}
const complex1: ComplexType = { value: 'foo' };
const complex2: ComplexType = { value: 'bar' };
const complexComparator: Comparator<ComplexType> = (a, b) =>
return a.value === b.value;
const hasChangedComplex = useHasChanged([
[ complex1, complex2, complexComparator ]
]);