21 lines
518 B
JavaScript
21 lines
518 B
JavaScript
|
// 3rd party.
|
||
|
import React, {useEffect, useState} from 'react';
|
||
|
|
||
|
export function usePropertyChange(object, property, defaultValue) {
|
||
|
const [value, setValue] = useState(defaultValue);
|
||
|
useEffect(() => {
|
||
|
if (!object) {
|
||
|
return;
|
||
|
}
|
||
|
const onValueChanged = () => {
|
||
|
setValue(object[property]);
|
||
|
};
|
||
|
onValueChanged();
|
||
|
object.on(`${property}Changed`, onValueChanged);
|
||
|
return () => {
|
||
|
object.off(`${property}Changed`, onValueChanged);
|
||
|
};
|
||
|
}, [object, value]);
|
||
|
return value;
|
||
|
}
|