{"name":"zustand","private":false,"version":"3.0.0-beta.0","description":"🐻 Bear necessities for state management in React","main":"index.cjs.js","module":"index.js","types":"src/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 [zombi child problem](https://react-redux.js.org/api/hooks#stale-props-and-zombie-children), [react concurreny](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/v8pjv251w7).\n\n    npm install zustand\n\n### First create a store\n\nYour store is a hook! You can put anything in it, atomics, 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 => ({ count: state.bears + 1 })),\n  resetPopulation: () => 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 into context providers\n* Can inform components transiently (without causing render)\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 foo or bar change\nconst { nuts, honey } = useStore(state => ({ nuts: state.nuts, honey: state.honey }), shallow)\n\n// Array pick, re-renders the component when either foo or bar change\nconst [nuts, honey] = useStore(state => [state.nuts, state.honey], shallow)\n\n// Mapped picks, re-renders the component when state.objects 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, it 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## 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## Reading/writing state and reacting to changes outside of components\n\nSometimes you need to access the 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(scratches => (scratchRef.current = scratches), state => state.scratches), [])\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  nested: { structure: { contains: { a: \"bear\" } } },\n  set: fn => set(produce(fn)),\n}))\n\nconst set = useStore(state => state.set)\nset(state => {\n  state.nested.structure.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.","readmeFilename":"readme.md","_id":"zustand@3.0.0-beta.0","_nodeVersion":"14.7.0","_npmVersion":"6.14.7","dist":{"integrity":"sha512-TV/+aBXQp7UWLj8Gsf+w1Yp4LHaScc1zdfF9GQL6Thd6Mg5aSYZavpBc2X4MjU3KhE0L2JuGl4IHzlRtpe2WDQ==","shasum":"72c152d886ff069682327d0aef34ef93476a897a","tarball":"http://registry-npm.ywwl.com/zustand/download/zustand-3.0.0-beta.0.tgz","fileCount":14,"unpackedSize":33315,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfOtySCRA9TVsSAnZWagAADCMP/ixvBQf9qH2BCN8TWP6i\n+8Im7So+NsY6q1wyynRomuDghS+2ZNar91/N7sIDBqNIXJbSEBGtjpJQ83Dw\ngZWvsiyzM9zDLa4Y+nH1pilzX3ZYXd0W+l/YlDrI0BF9mVDP8HYqFpWDsm+G\nQnW3DFyjVh3okxd+smR6vajfudzdOMQ27Ky+mGssFh59/hnAyJLx83AA1NIb\nnNTAtCyXgqvS6Xz9ItZhy4HKnlwQGRlxEudTaxv1xOKX0rK958Mv373tQ2GG\nl0JIktC+Bh3fnox3B4IgbwA3VJ7IBXxe8Hl/yez+Rhy1Zy3y3xQLkYfHr7MX\nBlKclBORYlxqfctfjIRHv4CpjOJnBHbJ6PL4gdNEtbMa2ytQe0q3rXxEenLp\nvX0lGKirBmI4o8mToMR92xXvNONZSKePic17ns7q9AY5FdeEE7rKAerSDq2X\nO2s+krMaNEu/Nz8eipGgNN+7KhG6u3JUCJiWi4gAGr4in+wqUc8UeznvA5SH\nUvfGLLhbHbH1OnXvNNxPSHjFFSzCbx4HxEnR7IzQBjaFOzGoTDRVGBV0HpMD\nXnbfGIXFIbb3J4NE7hQ21mvj9nXQ1Fg7MWhZ+13z4G2Zv5MrYMpsiRuX0zr2\nwEhdrJ60+dq6A4dnfpWbABWkymhpkGRBHsmTT7tkHxI1dRa3RQLawcNdoWOh\n7SDj\r\n=DkBZ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGI55sgEvdyk3QxpbnHAUppxmIwEGT9wfQ1T26N+cyw8AiAG5Quu5esr2zLuH8LeqHhR987fenDZzSzfQAMk2nMP/A=="}],"size":8887,"noattachment":false,"key":"/zustand/-/zustand-3.0.0-beta.0.tgz"},"maintainers":[{"name":"drcmda","email":"drcmda@gmail.com"}],"_npmUser":{"name":"drcmda","email":"drcmda@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/zustand_3.0.0-beta.0_1597693074002_0.11140739364680208"},"_hasShrinkwrap":false,"publish_time":1597693074148,"_cnpm_publish_time":1597693074148,"dist-tags":{"latest":"5.1.0"}}