{"name":"zustand","private":false,"version":"1.0.0-beta.0","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=\"700\" 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\n    npm install 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### How to use it\n\n#### First create a store (or multiple, up to you...)\n\nYour store is a hook! There are no rules, you can put anything in it, atomics, objects, functions. Like Reacts setState, `set` *merges* state, and it has the exact same semantics.\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 components with the resulting hook, 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? This lib ...\n\n1. is simpler and un-opinionated\n2. makes hooks the primary means of consuming state\n3. isn't strictly dependent on actions, types & dispatch\n4. doesn't wrap your app into context providers (which allows it to support [mixed reconcilers](https://github.com/konvajs/react-konva/issues/188))\n5. can access state outside of components (and even React)\n6. has a solution for rapid state changes (look below for transient updates)\n7. is (or can be made) compatible with the redux api\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 'zustand'\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## 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((set, get => ({ ... })))))\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.0","_nodeVersion":"12.4.0","_npmVersion":"6.9.0","dist":{"integrity":"sha512-EAPG9pBbNV7t3Ez+ohWzHP2v0FxWDq9H7lwsk+sPlGphPyWs6QbgGWxtxdkIFDeDdVxxk9WetFZAkgkXQ0T6+g==","shasum":"0bfd040581aca4180b7df16071e8c594156eeb88","tarball":"http://registry-npm.ywwl.com/zustand/download/zustand-1.0.0-beta.0.tgz","fileCount":10,"unpackedSize":21379,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdEf53CRA9TVsSAnZWagAAnYQP/3V0kx7+Ji6V0151TQHI\nDDvVBpCB2aw5YXADWG3w0IurjyqlmTkrHkl7Z4YBolPkF7MTmIa4ok1B3mRG\n10IrqcM6tsZHoA3vb/qgwHXnxQBnMxVLrQJTFJfksxpU8Vkc+NFvYZGpGEuO\nRusrZSAh1h/YKihu0ARm+AK7uxWwqV4ar9fr/uP9EZNgZHmA/FwgwRB0BVzQ\nAMA/gnXL7OM8vh27tR7GdvL69jFX0M4T5wj6bfVnw6Au8UAnwVU+EeHG1A2U\nGF7QFUSi9xz5/36iBxqADGLWSovVQkbAhDobAxXHYDcG8cesQ9PFjDA6QQg2\n5VgOMwnGLmhTr5Ni75pMvw5+VISzgzrEzaEqVpmdmmBjYYHQquEoCOi/T2dk\nSrD900YvDam1l6P5B9R85g0znncwprIT7W7bK91PsDo+ZwRY9eXlK0iqFefm\nh6dH8RxK62e9jLTMezGcY8mOYGll8B/bhPiGxcrzZMSEmfj2M11De9Smokwg\nallQLZzLNzHTIrRkSWZK6kH8mc0Ig5R/zhlUX3Yi1ZQOiUpR8SK/MCcXLQqA\n56ovonX+yBLnzd7y3gIL+9j0FaDfwuXSs/xRRaT0hPxLlisa0dCLI10eH7w1\nMgSyOGybqn6ck/qEfw8wHMVIZl5d3fWjNbxSFrkq2UuUnA12HoONQxYFt1UR\ntCPm\r\n=W2YJ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIB8RFbZMaxCA3GLdaxIIXKDPfUs1N0TakGjdAQeO6GD/AiEAoDQVsG8byKzIeVobzxlWv4AHCLxM5ckQom1ErFsi1yI="}],"size":7065,"noattachment":false,"key":"/zustand/-/zustand-1.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_1.0.0-beta.0_1561460342869_0.22575446707605296"},"_hasShrinkwrap":false,"publish_time":1561460342992,"_cnpm_publish_time":1561460342992,"dist-tags":{"latest":"5.1.0"}}