feat: Reducer for usePropertyChange

This commit is contained in:
cha0s 2019-04-24 17:00:50 -05:00
parent df2a876aea
commit 5b32d32a5e

View File

@ -1,14 +1,15 @@
// 3rd party.
import React, {useEffect, useState} from 'react';
export function usePropertyChange(object, property, defaultValue) {
export function usePropertyChange(object, property, defaultValue, reducer) {
const [value, setValue] = useState(defaultValue);
useEffect(() => {
if (!object) {
return;
}
const onValueChanged = () => {
setValue(object[property]);
const newValue = object[property];
setValue(reducer ? reducer(newValue) : newValue);
};
onValueChanged();
object.on(`${property}Changed`, onValueChanged);