UNPKG

react-hook-deep-state

Version:

![NPM Version](https://img.shields.io/npm/v/react-hook-deep-state) ![NPM Last Update](https://img.shields.io/npm/last-update/react-hook-deep-state) ![NPM Type Definitions](https://img.shields.io/npm/types/react-hook-deep-state) ![NPM Downloads](https://im

2 lines (1 loc) 1.02 kB
(function(r,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],l):(r=typeof globalThis<"u"?globalThis:r||self,l(r["react-hook-deep-state"]={},r.React))})(this,(function(r,l){"use strict";function n(i){return i!==null&&typeof i=="object"&&!Array.isArray(i)}function a(i,y){const s={...i};for(const c of Object.keys(y)){const e=y[c];if(n(e)){const f=s[c];n(f)?s[c]=a(f,e):s[c]=a({},e)}else s[c]=e}return s}function S(i){const[y,s]=l.useState(i);return[y,(e,f,j=!0)=>{if(!f){s(u=>{const t=e;return n(u)&&n(t)&&j?a({...u},t):t});return}f&&s(u=>{if(!n(u))return console.warn(`Cannot assign key-value pair. State must be of type "object", not "${typeof u}"`),u;const t=f.split("."),k=structuredClone(u);let o=k;for(let p=0;p<t.length-1;p++){const b=t[p];n(o[b])||(o[b]={}),o=o[b]}const d=t[t.length-1];return j&&n(o[d])&&n(e)?o[d]=a(o[d],e):o[d]=e,k})}]}r.useDeepState=S,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));