feat: Reducer for usePropertyChange
This commit is contained in:
parent
df2a876aea
commit
5b32d32a5e
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user