{"name":"zustand","private":false,"version":"3.1.0-beta.0","publishConfig":{"tag":"beta"},"description":"🐻 Bear necessities for state management in React","main":"index.cjs.js","module":"index.js","types":"index.d.ts","sideEffects":false,"repository":{"type":"git","url":"git+https://github.com/react-spring/zustand.git"},"keywords":["react","state","manager","management","redux","store"],"author":{"name":"Paul Henschel"},"contributors":[{"name":"Jeremy Holcomb","url":"https://github.com/JeremyRH"},{"name":"Daishi Kato","url":"https://github.com/dai-shi"}],"license":"MIT","bugs":{"url":"https://github.com/react-spring/zustand/issues"},"homepage":"https://github.com/react-spring/zustand","peerDependencies":{"react":">=16.8"},"readme":"<p align=\"center\">\n  <img width=\"500\" src=\"bear.png\" />\n</p>\n\n![Bundle Size](https://badgen.net/bundlephobia/minzip/zustand) [![Build Status](https://travis-ci.org/react-spring/zustand.svg?branch=master)](https://travis-ci.org/react-spring/zustand) [![npm version](https://badge.fury.io/js/zustand.svg)](https://badge.fury.io/js/zustand) ![npm](https://img.shields.io/npm/dt/zustand.svg)\n\nSmall, fast and scaleable bearbones state-management solution. Has a comfy api based on hooks, isn't boilerplatey or opinionated, but still just enough to be explicit and flux-like.\n\nDon't disregard it because it's cute. It has quite the claws, lots of time was spent to deal with common pitfalls, like the dreaded [zombie child problem](https://react-redux.js.org/api/hooks#stale-props-and-zombie-children), [react concurrency](https://github.com/bvaughn/rfcs/blob/useMutableSource/text/0000-use-mutable-source.md), and [context loss](https://github.com/facebook/react/issues/13332) between mixed renderers. It may be the one state-manager in the React space that gets all of these right.\n\nYou can try a live demo [here](https://codesandbox.io/s/dazzling-moon-itop4).\n\n```bash\nnpm install zustand\n```    \n\nZustand is pronounced \"tsoostand\" and means \"state\" in German.\n\n### First create a store\n\nYour store is a hook! You can put anything in it: primitives, objects, functions. The `set` function *merges* state.\n\n```jsx\nimport create from 'zustand'\n\nconst useStore = create(set => ({\n  bears: 0,\n  increasePopulation: () => set(state => ({ bears: state.bears + 1 })),\n  removeAllBears: () => set({ bears: 0 })\n}))\n```\n\n### Then bind your components, and that's it!\n\nUse the hook anywhere, no providers needed. Select your state and the component will re-render on changes.\n\n```jsx\nfunction BearCounter() {\n  const bears = useStore(state => state.bears)\n  return <h1>{bears} around here ...</h1>\n}\n\nfunction Controls() {\n  const increasePopulation = useStore(state => state.increasePopulation)\n  return <button onClick={increasePopulation}>one up</button>\n}\n```\n\n#### Why zustand over react-redux?\n\n* Simple and un-opinionated\n* Makes hooks the primary means of consuming state\n* Doesn't wrap your app in context providers\n* [Can inform components transiently (without causing render)](#transient-updates-for-often-occuring-state-changes)\n\n---\n\n# Recipes\n\n## Fetching everything\n\nYou can, but bear in mind that it will cause the component to update on every state change!\n\n```jsx\nconst state = useStore()\n```\n\n## Selecting multiple state slices\n\nIt detects changes with strict-equality (old === new) by default, this is efficient for atomic state picks.\n\n```jsx\nconst nuts = useStore(state => state.nuts)\nconst honey = useStore(state => state.honey)\n```\n\nIf you want to construct a single object with multiple state-picks inside, similar to redux's mapStateToProps, you can tell zustand that you want the object to be diffed shallowly by passing an alternative equality function.\n\n```jsx\nimport shallow from 'zustand/shallow'\n\n// Object pick, re-renders the component when either state.nuts or state.honey change\nconst { nuts, honey } = useStore(state => ({ nuts: state.nuts, honey: state.honey }), shallow)\n\n// Array pick, re-renders the component when either state.nuts or state.honey change\nconst [nuts, honey] = useStore(state => [state.nuts, state.honey], shallow)\n\n// Mapped picks, re-renders the component when state.treats changes in order, count or keys\nconst treats = useStore(state => Object.keys(state.treats), shallow)\n```\n\n## Fetching from multiple stores\n\nSince you can create as many stores as you like, forwarding results to succeeding selectors is as natural as it gets.\n\n```jsx\nconst currentBear = useCredentialsStore(state => state.currentBear)\nconst bear = useBearStore(state => state.bears[currentBear])\n```\n\n## Memoizing selectors\n\nIt is generally recommended to memoize selectors with useCallback. This will prevent unnecessary computations each render. It also allows React to optimize performance in concurrent mode.\n\n```jsx\nconst fruit = useStore(useCallback(state => state.fruits[id], [id]))\n```\n\nIf a selector doesn't depend on scope, you can define it outside the render function to obtain a fixed reference without useCallback.\n\n```jsx\nconst selector = state => state.berries\n\nfunction Component() {\n  const berries = useStore(selector)\n```\n\n## Overwriting state\n\nThe `set` function has a second argument, `false` by default. Instead of merging, it will replace the state model. Be careful not to wipe out parts you rely on, like actions.\n\n```jsx\nimport omit from \"lodash-es/omit\"\n\nconst useStore = create(set => ({\n  salmon: 1,\n  tuna: 2,\n  deleteEverything: () => set({ }), true), // clears the entire store, actions included\n  deleteTuna: () => set(state => omit(state, ['tuna']), true)\n}))\n```\n\n## Async actions\n\nJust call `set` when you're ready, zustand doesn't care if your actions are async or not.\n\n```jsx\nconst useStore = create(set => ({\n  fishies: {},\n  fetch: async pond => {\n    const response = await fetch(pond)\n    set({ fishies: await response.json() })\n  }\n}))\n```\n\n## Read from state in actions\n\n`set` allows fn-updates `set(state => result)`, but you still have access to state outside of it through `get`.\n\n```jsx\nconst useStore = create((set, get) => ({\n  sound: \"grunt\",\n  action: () => {\n    const sound = get().sound\n    // ...\n  }\n})\n```\n\n## Reading/writing state and reacting to changes outside of components\n\nSometimes you need to access state in a non-reactive way, or act upon the store. For these cases the resulting hook has utility functions attached to its prototype.\n\n```jsx\nconst useStore = create(() => ({ paw: true, snout: true, fur: true }))\n\n// Getting non-reactive fresh state\nconst paw = useStore.getState().paw\n// Listening to all changes, fires on every change\nconst unsub1 = useStore.subscribe(console.log)\n// Listening to selected changes, in this case when \"paw\" changes\nconst unsub2 = useStore.subscribe(console.log, state => state.paw)\n// Updating state, will trigger listeners\nuseStore.setState({ paw: false })\n// Unsubscribe listeners\nunsub1()\nunsub2()\n// Destroying the store (removing all listeners)\nuseStore.destroy()\n\n// You can of course use the hook as you always would\nfunction Component() {\n  const paw = useStore(state => state.paw)\n```\n\n## Using zustand without React\n\nZustands core can be imported and used without the React dependency. The only difference is that the create function does not return a hook, but the api utilities.\n\n```jsx\nimport create from 'zustand/vanilla'\n\nconst store = create(() => ({ ... }))\nconst { getState, setState, subscribe, destroy } = store\n```\n\nYou can even consume an existing vanilla store with React:\n\n```jsx\nimport create from 'zustand'\nimport vanillaStore from './vanillaStore'\n\nconst useStore = create(vanillaStore)\n```\n\n## Transient updates (for often occuring state-changes)\n\nThe subscribe function allows components to bind to a state-portion without forcing re-render on changes. Best combine it with useEffect for automatic unsubscribe on unmount. This can make a [drastic](https://codesandbox.io/s/peaceful-johnson-txtws) performance impact when you are allowed to mutate the view directly.\n\n```jsx\nconst useStore = create(set => ({ scratches: 0, ... }))\n\nfunction Component() {\n  // Fetch initial state\n  const scratchRef = useRef(useStore.getState().scratches)\n  // Connect to the store on mount, disconnect on unmount, catch state-changes in a reference\n  useEffect(() => useStore.subscribe(\n    scratches => (scratchRef.current = scratches), \n    state => state.scratches\n  ), [])\n```\n\n## Sick of reducers and changing nested state? Use Immer!\n\nReducing nested structures is tiresome. Have you tried [immer](https://github.com/mweststrate/immer)?\n\n```jsx\nimport produce from 'immer'\n\nconst useStore = create(set => ({\n  lush: { forrest: { contains: { a: \"bear\" } } },\n  set: fn => set(produce(fn)),\n}))\n\nconst set = useStore(state => state.set)\nset(state => {\n  state.lush.forrest.contains = null\n})\n```\n\n## Middleware\n\nYou can functionally compose your store any way you like.\n\n```jsx\n// Log every time state is changed\nconst log = config => (set, get, api) => config(args => {\n  console.log(\"  applying\", args)\n  set(args)\n  console.log(\"  new state\", get())\n}, get, api)\n\n// Turn the set method into an immer proxy\nconst immer = config => (set, get, api) => config(fn => set(produce(fn)), get, api)\n\nconst useStore = create(log(immer(set => ({\n  bees: false,\n  setBees: input => set(state => {\n    state.bees = input\n  })\n}))))\n```\n\n## Can't live without redux-like reducers and action types?\n\n```jsx\nconst types = { increase: \"INCREASE\", decrease: \"DECREASE\" }\n\nconst reducer = (state, { type, by = 1 }) => {\n  switch (type) {\n    case types.increase: return { grumpiness: state.grumpiness + by }\n    case types.decrease: return { grumpiness: state.grumpiness - by }\n  }\n}\n\nconst useStore = create(set => ({\n  grumpiness: 0,\n  dispatch: args => set(state => reducer(state, args)),\n}))\n\nconst dispatch = useStore(state => state.dispatch)\ndispatch({ type: types.increase, by: 2 })\n```\n\nOr, just use our redux-middleware. It wires up your main-reducer, sets initial state, and adds a dispatch function to the state itself and the vanilla api. Try [this](https://codesandbox.io/s/amazing-kepler-swxol) example.\n\n```jsx\nimport { redux } from 'zustand/middleware'\n\nconst useStore = create(redux(reducer, initialState))\n```\n\n## Redux devtools\n\n```jsx\nimport { devtools } from 'zustand/middleware'\n\n// Usage with a plain action store, it will log actions as \"setState\"\nconst useStore = create(devtools(store))\n// Usage with a redux store, it will log full action types\nconst useStore = create(devtools(redux(reducer, initialState)))\n```\n\ndevtools takes the store function as its first argument, optionally you can name the store with a second argument: `devtools(store, \"MyStore\")`, which will be prefixed to your actions.\n","readmeFilename":"readme.md","_id":"zustand@3.1.0-beta.0","_nodeVersion":"12.18.3","_npmVersion":"6.14.7","dist":{"integrity":"sha512-2/LFIrjnfsrBa1WjrpMEAYnWss2srMebswUzb/bJZRWmDptzvqYxzsLYe2SzCG4eoWQJ9shkqWPtyTyuAH+Gzg==","shasum":"7f65f784ed58d1268010b273546622bdb01bb401","tarball":"http://registry-npm.ywwl.com/zustand/download/zustand-3.1.0-beta.0.tgz","fileCount":14,"unpackedSize":35777,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfUMQTCRA9TVsSAnZWagAAd8YQAIoCA+npBsZ/MkiRc3e4\nMGpPt6lvgCQaBt6YlXIInBdhPvOJEGnxhDDj9a736egO5zofzi/QAKBMHrE+\n7PjFQ9ZUtSFeN4TWa4OK6IjvFKIc3mWYkpCbDlcandpxrevGym6iLp318exk\n+glT5CyAdFF6At3exlw51D2dKdazvIWK/Y6GEuAUJzuOWZ0ljo3Fhd0e73V4\ndTnq5XK+R7rV+FeyHctEWjjVmPOsPCXaU4qev21GIeXuziJ9q6uYk7Si1Jmh\nRlyERZ1nsfI27kcuhlSfC5B+2R+kjmRTNhSDIlxAAzWyCceOliX+snwuaedX\noKY+THYrDqdEPdg0csvvK11FzoallBqyTki1McW64Njx8oxvkjShddyCGjAA\ntsK9FloE4nzziahQ5OptAcWTJptcq59mTPHfc0cw2awrUe4MWExMliqqiTEU\nlip5Yc+XeifSRmMReR4PzdU/62ZyYFFiLRJpI0UBw+OF9ZTdY/SkNbd201/i\nEPmVU4ybJOVhF4PPi1N2QJ7VQBHUI/fb10WsgDOIMBpbwAplSkSyi0qZ8CFf\nF+dJkTnbBQpQG2lOFN4oFTsxPxJWlLhf0Gx6gL5U8/gU96CLXCozgmy98bvZ\nu2M5RBMqlADO7FW87swecUFy0w+TY1HNQWI5cYTOgy5zsRRU6C0qDzawFjCo\n5xCL\r\n=88sQ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCY0W3A1pc6H/JwzvALGlawtPfeE4P4w0SD8uQwd5cnigIhAKEcs+sCFix99Cgw65WS9AIdMVybI9rkm6VDqwRdOrWe"}],"size":9392,"noattachment":false,"key":"/zustand/-/zustand-3.1.0-beta.0.tgz"},"maintainers":[{"name":"drcmda","email":"drcmda@gmail.com"}],"_npmUser":{"name":"daishi","email":"daishi@axlight.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/zustand_3.1.0-beta.0_1599128595260_0.0238432955405965"},"_hasShrinkwrap":false,"publish_time":1599128595417,"_cnpm_publish_time":1599128595417,"dist-tags":{"latest":"5.1.0"}}