Socket
Socket
Sign inDemoInstall

zustand

Package Overview
Dependencies
Maintainers
3
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zustand - npm Package Compare versions

Comparing version 4.5.0 to 5.0.0-alpha.0

ts_version_4.5_and_above_is_required.d.ts

1

index.d.ts
export * from './vanilla';
export * from './react';
export { default } from './react';

@@ -9,15 +9,10 @@ 'use strict';

var useSyncExternalStoreWithSelector = useSyncExternalStoreExports.useSyncExternalStoreWithSelector;
var didWarnAboutEqualityFn = false;
var identity = function identity(arg) {
return arg;
};
function useStore(api, selector, equalityFn) {
function useStore(api, selector) {
if (selector === void 0) {
selector = identity;
}
if (process.env.NODE_ENV !== 'production' && equalityFn && !didWarnAboutEqualityFn) {
console.warn("[DEPRECATED] Use `createWithEqualityFn` instead of `create` or use `useStoreWithEqualityFn` instead of `useStore`. They can be imported from 'zustand/traditional'. https://github.com/pmndrs/zustand/discussions/1937");
didWarnAboutEqualityFn = true;
}
var slice = useSyncExternalStoreWithSelector(api.subscribe, api.getState, api.getServerState || api.getInitialState, selector, equalityFn);
var slice = useSyncExternalStoreWithSelector(api.subscribe, api.getState, api.getInitialState, selector);
useDebugValue(slice);

@@ -27,8 +22,5 @@ return slice;

var createImpl = function createImpl(createState) {
if (process.env.NODE_ENV !== 'production' && typeof createState !== 'function') {
console.warn("[DEPRECATED] Passing a vanilla store will be unsupported in a future version. Instead use `import { useStore } from 'zustand'`.");
}
var api = typeof createState === 'function' ? vanilla.createStore(createState) : createState;
var useBoundStore = function useBoundStore(selector, equalityFn) {
return useStore(api, selector, equalityFn);
var api = vanilla.createStore(createState);
var useBoundStore = function useBoundStore(selector) {
return useStore(api, selector);
};

@@ -41,11 +33,4 @@ Object.assign(useBoundStore, api);

};
var react = (function (createState) {
if (process.env.NODE_ENV !== 'production') {
console.warn("[DEPRECATED] Default export is deprecated. Instead use `import { create } from 'zustand'`.");
}
return create(createState);
});
exports.create = create;
exports.default = react;
exports.useStore = useStore;

@@ -58,7 +43,1 @@ Object.keys(vanilla).forEach(function (k) {

});
module.exports = react;
module.exports.create = create;
module.exports.useStore = useStore;
module.exports.createStore = vanilla.createStore;
exports.default = module.exports;

@@ -347,135 +347,5 @@ 'use strict';

};
var oldImpl = function oldImpl(config, baseOptions) {
var persistImpl = function persistImpl(config, baseOptions) {
return function (set, get, api) {
var options = _extends({
getStorage: function getStorage() {
return localStorage;
},
serialize: JSON.stringify,
deserialize: JSON.parse,
partialize: function partialize(state) {
return state;
},
version: 0,
merge: function merge(persistedState, currentState) {
return _extends({}, currentState, persistedState);
}
}, baseOptions);
var _hasHydrated = false;
var hydrationListeners = new Set();
var finishHydrationListeners = new Set();
var storage;
try {
storage = options.getStorage();
} catch (e) {}
if (!storage) {
return config(function () {
console.warn("[zustand persist middleware] Unable to update item '" + options.name + "', the given storage is currently unavailable.");
set.apply(void 0, arguments);
}, get, api);
}
var thenableSerialize = toThenable(options.serialize);
var setItem = function setItem() {
var state = options.partialize(_extends({}, get()));
var errorInSync;
var thenable = thenableSerialize({
state: state,
version: options.version
}).then(function (serializedValue) {
return storage.setItem(options.name, serializedValue);
}).catch(function (e) {
errorInSync = e;
});
if (errorInSync) {
throw errorInSync;
}
return thenable;
};
var savedSetState = api.setState;
api.setState = function (state, replace) {
savedSetState(state, replace);
void setItem();
};
var configResult = config(function () {
set.apply(void 0, arguments);
void setItem();
}, get, api);
var stateFromStorage;
var hydrate = function hydrate() {
if (!storage) return;
_hasHydrated = false;
hydrationListeners.forEach(function (cb) {
return cb(get());
});
var postRehydrationCallback = (options.onRehydrateStorage == null ? void 0 : options.onRehydrateStorage(get())) || undefined;
return toThenable(storage.getItem.bind(storage))(options.name).then(function (storageValue) {
if (storageValue) {
return options.deserialize(storageValue);
}
}).then(function (deserializedStorageValue) {
if (deserializedStorageValue) {
if (typeof deserializedStorageValue.version === 'number' && deserializedStorageValue.version !== options.version) {
if (options.migrate) {
return options.migrate(deserializedStorageValue.state, deserializedStorageValue.version);
}
console.error("State loaded from storage couldn't be migrated since no migrate function was provided");
} else {
return deserializedStorageValue.state;
}
}
}).then(function (migratedState) {
var _get;
stateFromStorage = options.merge(migratedState, (_get = get()) != null ? _get : configResult);
set(stateFromStorage, true);
return setItem();
}).then(function () {
postRehydrationCallback == null || postRehydrationCallback(stateFromStorage, undefined);
_hasHydrated = true;
finishHydrationListeners.forEach(function (cb) {
return cb(stateFromStorage);
});
}).catch(function (e) {
postRehydrationCallback == null || postRehydrationCallback(undefined, e);
});
};
api.persist = {
setOptions: function setOptions(newOptions) {
options = _extends({}, options, newOptions);
if (newOptions.getStorage) {
storage = newOptions.getStorage();
}
},
clearStorage: function clearStorage() {
var _storage;
(_storage = storage) == null || _storage.removeItem(options.name);
},
getOptions: function getOptions() {
return options;
},
rehydrate: function rehydrate() {
return hydrate();
},
hasHydrated: function hasHydrated() {
return _hasHydrated;
},
onHydrate: function onHydrate(cb) {
hydrationListeners.add(cb);
return function () {
hydrationListeners.delete(cb);
};
},
onFinishHydration: function onFinishHydration(cb) {
finishHydrationListeners.add(cb);
return function () {
finishHydrationListeners.delete(cb);
};
}
};
hydrate();
return stateFromStorage || configResult;
};
};
var newImpl = function newImpl(config, baseOptions) {
return function (set, get, api) {
var options = _extends({
storage: createJSONStorage(function () {

@@ -492,3 +362,3 @@ return localStorage;

}, baseOptions);
var _hasHydrated2 = false;
var _hasHydrated = false;
var hydrationListeners = new Set();

@@ -524,10 +394,10 @@ var finishHydrationListeners = new Set();

var hydrate = function hydrate() {
var _get3;
var _get2;
if (!storage) return;
_hasHydrated2 = false;
_hasHydrated = false;
hydrationListeners.forEach(function (cb) {
var _get2;
return cb((_get2 = get()) != null ? _get2 : configResult);
var _get;
return cb((_get = get()) != null ? _get : configResult);
});
var postRehydrationCallback = (options.onRehydrateStorage == null ? void 0 : options.onRehydrateStorage((_get3 = get()) != null ? _get3 : configResult)) || undefined;
var postRehydrationCallback = (options.onRehydrateStorage == null ? void 0 : options.onRehydrateStorage((_get2 = get()) != null ? _get2 : configResult)) || undefined;
return toThenable(storage.getItem.bind(storage))(options.name).then(function (deserializedStorageValue) {

@@ -545,4 +415,4 @@ if (deserializedStorageValue) {

}).then(function (migratedState) {
var _get4;
stateFromStorage = options.merge(migratedState, (_get4 = get()) != null ? _get4 : configResult);
var _get3;
stateFromStorage = options.merge(migratedState, (_get3 = get()) != null ? _get3 : configResult);
set(stateFromStorage, true);

@@ -553,3 +423,3 @@ return setItem();

stateFromStorage = get();
_hasHydrated2 = true;
_hasHydrated = true;
finishHydrationListeners.forEach(function (cb) {

@@ -570,4 +440,4 @@ return cb(stateFromStorage);

clearStorage: function clearStorage() {
var _storage2;
(_storage2 = storage) == null || _storage2.removeItem(options.name);
var _storage;
(_storage = storage) == null || _storage.removeItem(options.name);
},

@@ -581,3 +451,3 @@ getOptions: function getOptions() {

hasHydrated: function hasHydrated() {
return _hasHydrated2;
return _hasHydrated;
},

@@ -603,11 +473,2 @@ onHydrate: function onHydrate(cb) {

};
var persistImpl = function persistImpl(config, baseOptions) {
if ('getStorage' in baseOptions || 'serialize' in baseOptions || 'deserialize' in baseOptions) {
if (process.env.NODE_ENV !== 'production') {
console.warn('[DEPRECATED] `getStorage`, `serialize` and `deserialize` options are deprecated. Use `storage` option instead.');
}
return oldImpl(config, baseOptions);
}
return newImpl(config, baseOptions);
};
var persist = persistImpl;

@@ -614,0 +475,0 @@

@@ -25,28 +25,2 @@ import type { StateCreator, StoreMutatorIdentifier } from '../vanilla';

/**
* @deprecated Use `storage` instead.
* A function returning a storage.
* The storage must fit `window.localStorage`'s api (or an async version of it).
* For example the storage could be `AsyncStorage` from React Native.
*
* @default () => localStorage
*/
getStorage?: () => StateStorage;
/**
* @deprecated Use `storage` instead.
* Use a custom serializer.
* The returned string will be stored in the storage.
*
* @default JSON.stringify
*/
serialize?: (state: StorageValue<S>) => string | Promise<string>;
/**
* @deprecated Use `storage` instead.
* Use a custom deserializer.
* Must return an object matching StorageValue<S>
*
* @param str The storage's current value.
* @default JSON.parse
*/
deserialize?: (str: string) => StorageValue<PersistedState> | Promise<StorageValue<PersistedState>>;
/**
* Use a custom persist storage.

@@ -53,0 +27,0 @@ *

{
"name": "zustand",
"private": false,
"version": "4.5.0",
"version": "5.0.0-alpha.0",
"publishConfig": {
"tag": "next"
},
"description": "🐻 Bear necessities for state management in React",

@@ -9,14 +12,19 @@ "main": "./index.js",

"typesVersions": {
"<4.0": {
">=4.5": {
"esm/*": [
"ts3.4/*"
"esm/*"
],
"*": [
"ts3.4/*"
"*"
]
},
"*": {
"esm/*": [
"ts_version_4.5_and_above_is_required.d.ts"
],
"*": [
"ts_version_4.5_and_above_is_required.d.ts"
]
}
},
"files": [
"**"
],
"exports": {

@@ -29,6 +37,2 @@ "./package.json": "./package.json",

},
"module": {
"types": "./esm/index.d.ts",
"default": "./esm/index.js"
},
"default": {

@@ -39,118 +43,19 @@ "types": "./index.d.ts",

},
"./vanilla": {
"./*": {
"import": {
"types": "./esm/vanilla.d.mts",
"default": "./esm/vanilla.mjs"
"types": "./esm/*.d.mts",
"default": "./esm/*.mjs"
},
"module": {
"types": "./esm/vanilla.d.ts",
"default": "./esm/vanilla.js"
},
"default": {
"types": "./vanilla.d.ts",
"default": "./vanilla.js"
"types": "./*.d.ts",
"default": "./*.js"
}
},
"./middleware": {
"import": {
"types": "./esm/middleware.d.mts",
"default": "./esm/middleware.mjs"
},
"module": {
"types": "./esm/middleware.d.ts",
"default": "./esm/middleware.js"
},
"default": {
"types": "./middleware.d.ts",
"default": "./middleware.js"
}
},
"./middleware/immer": {
"import": {
"types": "./esm/middleware/immer.d.mts",
"default": "./esm/middleware/immer.mjs"
},
"module": {
"types": "./esm/middleware/immer.d.ts",
"default": "./esm/middleware/immer.js"
},
"default": {
"types": "./middleware/immer.d.ts",
"default": "./middleware/immer.js"
}
},
"./shallow": {
"import": {
"types": "./esm/shallow.d.mts",
"default": "./esm/shallow.mjs"
},
"module": {
"types": "./esm/shallow.d.ts",
"default": "./esm/shallow.js"
},
"default": {
"types": "./shallow.d.ts",
"default": "./shallow.js"
}
},
"./vanilla/shallow": {
"import": {
"types": "./esm/vanilla/shallow.d.mts",
"default": "./esm/vanilla/shallow.mjs"
},
"module": {
"types": "./esm/vanilla/shallow.d.ts",
"default": "./esm/vanilla/shallow.js"
},
"default": {
"types": "./vanilla/shallow.d.ts",
"default": "./vanilla/shallow.js"
}
},
"./react/shallow": {
"import": {
"types": "./esm/react/shallow.d.mts",
"default": "./esm/react/shallow.mjs"
},
"module": {
"types": "./esm/react/shallow.d.ts",
"default": "./esm/react/shallow.js"
},
"default": {
"types": "./react/shallow.d.ts",
"default": "./react/shallow.js"
}
},
"./traditional": {
"import": {
"types": "./esm/traditional.d.mts",
"default": "./esm/traditional.mjs"
},
"module": {
"types": "./esm/traditional.d.ts",
"default": "./esm/traditional.js"
},
"default": {
"types": "./traditional.d.ts",
"default": "./traditional.js"
}
},
"./context": {
"import": {
"types": "./esm/context.d.mts",
"default": "./esm/context.mjs"
},
"module": {
"types": "./esm/context.d.ts",
"default": "./esm/context.js"
},
"default": {
"types": "./context.d.ts",
"default": "./context.js"
}
}
},
"files": [
"**"
],
"sideEffects": false,
"engines": {
"node": ">=12.7.0"
"node": ">=12.20.0"
},

@@ -179,9 +84,7 @@ "repository": {

"homepage": "https://github.com/pmndrs/zustand",
"dependencies": {
"use-sync-external-store": "1.2.0"
},
"peerDependencies": {
"@types/react": ">=16.8",
"@types/react": ">=18.0",
"immer": ">=9.0.6",
"react": ">=16.8"
"react": ">=18.0",
"use-sync-external-store": ">=1.2.0"
},

@@ -197,4 +100,7 @@ "peerDependenciesMeta": {

"optional": true
},
"use-sync-external-store": {
"optional": true
}
}
}

@@ -6,20 +6,7 @@ import type { Mutate, StateCreator, StoreApi, StoreMutatorIdentifier } from './vanilla';

type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>;
type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {
/** @deprecated please use api.getState() */
getServerState?: () => ExtractState<S>;
};
export declare function useStore<S extends WithReact<StoreApi<unknown>>>(api: S): ExtractState<S>;
export declare function useStore<S extends WithReact<StoreApi<unknown>>, U>(api: S, selector: (state: ExtractState<S>) => U): U;
/**
* @deprecated Use `useStoreWithEqualityFn` from 'zustand/traditional'
* https://github.com/pmndrs/zustand/discussions/1937
*/
export declare function useStore<S extends WithReact<StoreApi<unknown>>, U>(api: S, selector: (state: ExtractState<S>) => U, equalityFn: ((a: U, b: U) => boolean) | undefined): U;
export type UseBoundStore<S extends WithReact<ReadonlyStoreApi<unknown>>> = {
export declare function useStore<S extends StoreApi<unknown>>(api: S): ExtractState<S>;
export declare function useStore<S extends StoreApi<unknown>, U>(api: S, selector: (state: ExtractState<S>) => U): U;
export type UseBoundStore<S extends ReadonlyStoreApi<unknown>> = {
(): ExtractState<S>;
<U>(selector: (state: ExtractState<S>) => U): U;
/**
* @deprecated Use `createWithEqualityFn` from 'zustand/traditional'
*/
<U>(selector: (state: ExtractState<S>) => U, equalityFn: (a: U, b: U) => boolean): U;
} & S;

@@ -29,12 +16,4 @@ type Create = {

<T>(): <Mos extends [StoreMutatorIdentifier, unknown][] = []>(initializer: StateCreator<T, [], Mos>) => UseBoundStore<Mutate<StoreApi<T>, Mos>>;
/**
* @deprecated Use `useStore` hook to bind store
*/
<S extends StoreApi<unknown>>(store: S): UseBoundStore<S>;
};
export declare const create: Create;
/**
* @deprecated Use `import { create } from 'zustand'`
*/
declare const _default: Create;
export default _default;
export {};

@@ -1,7 +0,2 @@

import { shallow } from './vanilla/shallow';
/**
* @deprecated Use `import { shallow } from 'zustand/shallow'`
*/
declare const _default: typeof shallow;
export default _default;
export { shallow };
export { shallow } from './vanilla/shallow';
export { useShallow } from './react/shallow';
'use strict';
var ReactExports = require('react');
function _unsupportedIterableToArray(o, minLen) {

@@ -35,3 +37,3 @@ if (!o) return;

function shallow$1(objA, objB) {
function shallow(objA, objB) {
if (Object.is(objA, objB)) {

@@ -77,14 +79,12 @@ return true;

var shallow = (function (objA, objB) {
if (process.env.NODE_ENV !== 'production') {
console.warn("[DEPRECATED] Default export is deprecated. Instead use `import { shallow } from 'zustand/shallow'`.");
}
return shallow$1(objA, objB);
});
var useRef = ReactExports.useRef;
function useShallow(selector) {
var prev = useRef();
return function (state) {
var next = selector(state);
return shallow(prev.current, next) ? prev.current : prev.current = next;
};
}
exports.default = shallow;
exports.shallow = shallow$1;
module.exports = shallow;
module.exports.shallow = shallow$1;
exports.default = module.exports;
exports.shallow = shallow;
exports.useShallow = useShallow;

@@ -6,9 +6,5 @@ import type { Mutate, StateCreator, StoreApi, StoreMutatorIdentifier } from './vanilla';

type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>;
type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {
/** @deprecated please use api.getState() */
getServerState?: () => ExtractState<S>;
};
export declare function useStoreWithEqualityFn<S extends WithReact<StoreApi<unknown>>>(api: S): ExtractState<S>;
export declare function useStoreWithEqualityFn<S extends WithReact<StoreApi<unknown>>, U>(api: S, selector: (state: ExtractState<S>) => U, equalityFn?: (a: U, b: U) => boolean): U;
export type UseBoundStoreWithEqualityFn<S extends WithReact<ReadonlyStoreApi<unknown>>> = {
export declare function useStoreWithEqualityFn<S extends StoreApi<unknown>>(api: S): ExtractState<S>;
export declare function useStoreWithEqualityFn<S extends StoreApi<unknown>, U>(api: S, selector: (state: ExtractState<S>) => U, equalityFn?: (a: U, b: U) => boolean): U;
export type UseBoundStoreWithEqualityFn<S extends ReadonlyStoreApi<unknown>> = {
(): ExtractState<S>;

@@ -15,0 +11,0 @@ <U>(selector: (state: ExtractState<S>) => U, equalityFn?: (a: U, b: U) => boolean): U;

@@ -16,3 +16,3 @@ 'use strict';

}
var slice = useSyncExternalStoreWithSelector(api.subscribe, api.getState, api.getServerState || api.getInitialState, selector, equalityFn);
var slice = useSyncExternalStoreWithSelector(api.subscribe, api.getState, api.getInitialState, selector, equalityFn);
useDebugValue(slice);

@@ -19,0 +19,0 @@ return slice;

@@ -11,6 +11,2 @@ type SetStateInternal<T> = {

subscribe: (listener: (state: T, prevState: T) => void) => () => void;
/**
* @deprecated Use `unsubscribe` returned by `subscribe`
*/
destroy: () => void;
}

@@ -30,54 +26,2 @@ type Get<T, K, F> = K extends keyof T ? T[K] : F;

export declare const createStore: CreateStore;
/**
* @deprecated Use `import { createStore } from 'zustand/vanilla'`
*/
declare const _default: CreateStore;
export default _default;
/**
* @deprecated Use `unknown` instead of `State`
*/
export type State = unknown;
/**
* @deprecated Use `Partial<T> | ((s: T) => Partial<T>)` instead of `PartialState<T>`
*/
export type PartialState<T extends State> = Partial<T> | ((state: T) => Partial<T>);
/**
* @deprecated Use `(s: T) => U` instead of `StateSelector<T, U>`
*/
export type StateSelector<T extends State, U> = (state: T) => U;
/**
* @deprecated Use `(a: T, b: T) => boolean` instead of `EqualityChecker<T>`
*/
export type EqualityChecker<T> = (state: T, newState: T) => boolean;
/**
* @deprecated Use `(state: T, previousState: T) => void` instead of `StateListener<T>`
*/
export type StateListener<T> = (state: T, previousState: T) => void;
/**
* @deprecated Use `(slice: T, previousSlice: T) => void` instead of `StateSliceListener<T>`.
*/
export type StateSliceListener<T> = (slice: T, previousSlice: T) => void;
/**
* @deprecated Use `(listener: (state: T) => void) => void` instead of `Subscribe<T>`.
*/
export type Subscribe<T extends State> = {
(listener: (state: T, previousState: T) => void): () => void;
};
/**
* @deprecated You might be looking for `StateCreator`, if not then
* use `StoreApi<T>['setState']` instead of `SetState<T>`.
*/
export type SetState<T extends State> = {
_(partial: T | Partial<T> | {
_(state: T): T | Partial<T>;
}['_'], replace?: boolean | undefined): void;
}['_'];
/**
* @deprecated You might be looking for `StateCreator`, if not then
* use `StoreApi<T>['getState']` instead of `GetState<T>`.
*/
export type GetState<T extends State> = () => T;
/**
* @deprecated Use `StoreApi<T>['destroy']` instead of `Destroy`.
*/
export type Destroy = () => void;
export {};

@@ -9,6 +9,6 @@ 'use strict';

if (!Object.is(nextState, state)) {
var _previousState = state;
var previousState = state;
state = (replace != null ? replace : typeof nextState !== 'object' || nextState === null) ? nextState : Object.assign({}, state, nextState);
listeners.forEach(function (listener) {
return listener(state, _previousState);
return listener(state, previousState);
});

@@ -29,8 +29,2 @@ }

};
var destroy = function destroy() {
if (process.env.NODE_ENV !== 'production') {
console.warn('[DEPRECATED] The `destroy` method will be unsupported in a future version. Instead use unsubscribe function returned by subscribe. Everything will be garbage-collected if store is garbage-collected.');
}
listeners.clear();
};
var api = {

@@ -40,4 +34,3 @@ setState: setState,

getInitialState: getInitialState,
subscribe: subscribe,
destroy: destroy
subscribe: subscribe
};

@@ -50,14 +43,3 @@ var initialState = state = createState(setState, getState, api);

};
var vanilla = (function (createState) {
if (process.env.NODE_ENV !== 'production') {
console.warn("[DEPRECATED] Default export is deprecated. Instead use import { createStore } from 'zustand/vanilla'.");
}
return createStore(createState);
});
exports.createStore = createStore;
exports.default = vanilla;
module.exports = vanilla;
module.exports.createStore = createStore;
exports.default = module.exports;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc