@fruits-chain/react-native-xiaoshu
Version:
React Native UI library
31 lines (27 loc) • 783 B
JavaScript
import { useState, useCallback } from 'react';
import { isFunction, isObject } from '../helpers';
import useDestroyed from './useDestroyed';
/**
* useState 类似 this.setState 可以传入部分字段更新
* @param state 状态
*/
const useStateUpdate = state => {
const [localState, setLocalState] = useState(state);
const getDestroyed = useDestroyed();
const updateState = useCallback(s => {
if (!getDestroyed()) {
setLocalState(ls => {
const value = isFunction(s) ? s(ls) : s;
if (isObject(ls)) {
return { ...ls,
...value
};
}
return value;
});
}
}, [getDestroyed]);
return [localState, updateState];
};
export default useStateUpdate;
//# sourceMappingURL=useStateUpdate.js.map