{"name":"zustand","private":false,"version":"1.0.0-beta.1","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/drcmda/zustand.git"},"keywords":["react","state","manager","management","redux","store"],"author":{"name":"Paul Henschel"},"license":"MIT","bugs":{"url":"https://github.com/drcmda/zustand/issues"},"homepage":"https://github.com/drcmda/zustand#readme","peerDependencies":{"react":">=16.8"},"readme":"<p align=\"center\">\n  <img width=\"500\" src=\"bear.png\" />\n</p>\n\n[![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)\n\nSmall, fast and scaleable bearbones state-management solution. Has a comfy api based on hooks, isn't that boilerplatey or opinionated, but still just enough to be explicit and flux-like. Try a small 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. Like Reacts setState, `set` *merges* state.\n\n```jsx\nimport create from 'zustand'\n\nconst [useStore] = create(set => ({\n  count: 0,\n  increase: () => set(state => ({ count: state.count + 1 })),\n  reset: () => set({ count: 0 })\n}))\n```\n\n### Then bind your components, that's it!\n\nUse the hook anywhere, no providers needed. Once you have selected state your component will re-render on changes.\n\n```jsx\nfunction Counter() {\n  const count = useStore(state => state.count)\n  return <h1>{count}</h1>\n}\n\nfunction Controls() {\n  const increase = useStore(state => state.increase)\n  return <button onClick={increase}>up</button>\n}\n```\n\n#### Why zustand over react-redux?\n\n* Simpler 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 remember 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\nzustand defaults to strict-equality (old === new) to detect changes, this is efficient for atomic state picks. \n\n```jsx\nconst foo = useStore(state => state.foo)\nconst bar = useStore(state => state.bar)\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.\n\n```jsx\nimport shallowequal from 'shallowequal'\n\nconst { foo, bar } = useStore(state => ({ foo: state.foo, bar: state.bar }), shallowequal)\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 currentUser = useCredentialsStore(state => state.currentUser)\nconst person = usePersonStore(state => state.persons[currentUser])\n```\n\n## Memoizing selectors\n\nSelectors run on state changes, as well as when the component renders. If you give zustand a fixed reference it will only run on state changes, or when the selector changes. Don't worry about this, unless your selector is expensive.\n\n```js\nconst fooSelector = useCallback(state => state.foo[props.id], [props.id])\nconst foo = useStore(fooSelector)\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  json: {},\n  fetch: async url => {\n    const response = await fetch(url)\n    set({ json: 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  text: \"hello\",\n  action: () => {\n    const text = get().text\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: \"value\" } } },\n  set: fn => set(produce(fn)),\n}))\n\nconst set = useStore(state => state.set)\nset(state => void state.nested.structure.contains = null)\n```\n\n## Reading/writing state and reacting to changes outside of components\n\nYou can use it with or without React out of the box.\n\n```jsx\nconst [, api] = create({ a: 1, b: 2, c: 3 })\n\n// Getting fresh state\nconst num = api.getState().n\n// Listening to all changes, fires on every dispatch\nconst unsub1 = api.subscribe(state => console.log(\"state changed\", state))\n// Listening to selected changes\nconst unsub2 = api.subscribe(state => state.a, a => console.log(\"a changed\", a))\n// Updating state, will trigger listeners\napi.setState({ a: 1 })\n// Unsubscribe listeners\nunsub1()\nunsub2()\n// Destroying the store (removing all listeners)\napi.destroy()\n```\n\n## Transient updates (for often occuring state-changes)\n\nThe api signature of subscribe([selector,] callback):unsub allows you to easily bind a component to a store without forcing it to re-render on state changes, you will be notified in a callback instead. Best combine it with useEffect. This can make a [drastic](https://codesandbox.io/s/peaceful-johnson-txtws) performance difference when you are allowed to mutate the view directly.\n\n```jsx\nconst [useStore, api] = create(set => ({ [0]: [-10, 0], [1]: [10, 5], ... }))\n\nfunction Component({ id }) {\n  // Fetch initial state\n  const xy = useRef(api.getState()[id])\n  // Connect to the store on mount, disconnect on unmount, catch state-changes in a callback\n  useEffect(() => api.subscribe(state => state[id], coords => (xy.current = coords)), [id])\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  text: \"hello\",\n  setText: input => set(state => {\n    state.text = 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 { count: state.count + by }\n    case types.decrease: return { count: state.count - by }\n  }\n}\n\nconst [useStore] = create(set => ({\n  count: 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","readmeFilename":"readme.md","_id":"zustand@1.0.0-beta.1","_nodeVersion":"12.4.0","_npmVersion":"6.9.0","dist":{"integrity":"sha512-m69L9/Bwv3848CHklcVfHSZcIKz3HMmEmr7SQm+2uySau1QpxVpZYq6pZ2uJtUX5fsQQHT1qgtLRzi5ys4kTAg==","shasum":"3bff8a5626e2885c4683fc66c84d4f4e25be956f","tarball":"http://registry-npm.ywwl.com/zustand/download/zustand-1.0.0-beta.1.tgz","fileCount":10,"unpackedSize":21122,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdFhOFCRA9TVsSAnZWagAAHY4P/i08lsASVR2wFpvLy15l\nH2z32RlSrvdx2EvNUs8AdFnVbk/tb+Abawn4F7rC1LwIiovV1YX4NGpWI/iC\nJs+MBAt3Ymli2I28DlopwUnNqIngawf5DDbSx+Gr+h26S06D3vrFOa1VmaYY\nZwajDbSGX91YDkIusRLVQXIbE7FdV6ggEN60is9WEDqZmiXGjCDtIn4k7kqf\niRuVSeVNHVW3oSNi3cXFpq2KKYXsmHekrNEbOqZGGav5EJ869dbCJaHYv9Yz\n33+CVLIGSLFV0JbuRevpHqTemKmB5HAFFJ8nyK7uZyFHXLbm7ouDJA52vPmr\n1lcHAI5ZdhlpCRCEwUVolifX5teVLRbvVFawNL8hubeXP/aHw+nMzSTaPbiJ\noZcRssjKbF7Nnz/GrOIOmPLJxCEmUptlydfQWvFCObc/DQ1dJ11apmADvAHI\nXeh9Ujd88EmAf0atDdc+8gyqdPgTUW7/EZklvUlFH1J4jDBXf6db26ujfgix\nSS429HBpPGJp8nymUH27S5t6hEes5Q/2Hk6D26kJDXmGVC8USbJSVjRnxXGX\nNDGmOHnF57Fg3TFSGIQabgonzjk7MpiY4w6fKuik0MGB4J1BvS6aGwxEnHfj\nMTJ4/ItjCnScln/4eQhTSXl+f2W8qD9qfyPVqf3sYDimb3aT65/sapel0zG2\nH693\r\n=/NNW\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCO5HMBENgp/HrJ9bCgfo3dqfnW1DMlRCFZLJ1q3N8MxQIhAKj9X//J3Zn56Vy92yFgL1sPPhV/huBG5qbxYjlNWUiG"}],"size":6894,"noattachment":false,"key":"/zustand/-/zustand-1.0.0-beta.1.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_1.0.0-beta.1_1561727876641_0.8584602773469214"},"_hasShrinkwrap":false,"publish_time":1561727876783,"_cnpm_publish_time":1561727876783,"dist-tags":{"latest":"5.1.0"}}