UNPKG

@darwish/hooks-core

Version:

29 lines (28 loc) 998 B
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { useCallback, useState } from 'react'; /** * @description * @param initialValue The initial state of the component * @returns A tuple with two elements * @module hooks/core/useSetState * @see */ export default function useSetState(initialValue) { var _a = useState(initialValue), state = _a[0], setState = _a[1]; var update = useCallback(function (updateValue) { setState(function (prevState) { return (__assign(__assign({}, prevState), (typeof updateValue === 'function' ? updateValue(prevState) : updateValue))); }); }, []); return [state, update]; }