UNPKG

@darwish/hooks-core

Version:

32 lines (31 loc) 1.1 kB
"use strict"; 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); }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = require("react"); /** * @description * @param initialValue The initial state of the component * @returns A tuple with two elements * @module hooks/core/useSetState * @see */ function useSetState(initialValue) { var _a = (0, react_1.useState)(initialValue), state = _a[0], setState = _a[1]; var update = (0, react_1.useCallback)(function (updateValue) { setState(function (prevState) { return (__assign(__assign({}, prevState), (typeof updateValue === 'function' ? updateValue(prevState) : updateValue))); }); }, []); return [state, update]; } exports.default = useSetState;