{"name":"zustand","version":"0.2.2-beta.0","description":"🐻 Bear necessities for state management in React","main":"dist/index.cjs.js","module":"dist/index.js","types":"dist/index.d.ts","sideEffects":false,"scripts":{"prebuild":"rimraf dist","build":"rollup -c","prepare":"npm run build --scripts-prepend-node-path","test":"jest","test:dev":"jest --watch --no-coverage","test:coverage:watch":"jest --watch"},"husky":{"hooks":{"pre-commit":"lint-staged"}},"prettier":{"semi":false,"trailingComma":"es5","singleQuote":true,"jsxBracketSameLine":true,"tabWidth":2,"printWidth":80},"lint-staged":{"*.{js,ts,tsx}":["prettier --write","git add"]},"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","jest":{"testPathIgnorePatterns":["/node_modules/","jest","legacy"],"testRegex":"test.(js|ts|tsx)$","coverageDirectory":"./coverage/","collectCoverage":true,"coverageReporters":["json","html","text","text-summary"],"collectCoverageFrom":["src/**/*.{js,ts,tsx}","tests/**/*.{js,ts,tsx}"]},"devDependencies":{"@babel/core":"^7.4.3","@babel/plugin-transform-modules-commonjs":"^7.4.3","@babel/plugin-transform-react-jsx":"^7.3.0","@babel/plugin-transform-typescript":"^7.4.0","@babel/preset-env":"^7.4.3","@testing-library/react":"^8.0.1","@types/jest":"^24.0.11","@types/react":"^16.8.13","husky":"^2.3.0","jest":"^24.8.0","lint-staged":"^8.1.5","prettier":"^1.17.0","react":"^16.8.6","react-dom":"^16.8.6","rimraf":"^2.6.3","rollup":"^1.13.1","rollup-plugin-babel":"^4.3.2","rollup-plugin-node-resolve":"^5.0.1","rollup-plugin-size-snapshot":"^0.9.0","rollup-plugin-typescript2":"^0.21.1","typescript":"^3.5.1"},"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\nJust like with Redux's mapStateToProps, useStore can select state, either atomically or by returning an object. It will run a small shallow-equal test over the results you return and update the component on changes only.\n\n```jsx\nconst { foo, bar } = useStore(state => ({ foo: state.foo, bar: state.bar }))\n```\n\nAtomic selects do the same ...\n\n```jsx\nconst foo = useStore(state => state.foo)\nconst bar = useStore(state => state.bar)\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, optimizing performance\n\nSay you select a piece of state ...\n\n```js\nconst foo = useStore(state => state.foo[props.id])\n```\n\nYour selector (`state => state.foo[props.id]`) will run on every state change, as well as every time the component renders. It isn't that expensive in this case, but let's optimize it for arguments sake.\n\nYou can either pass a static reference:\n\n```js\nconst fooSelector = useCallback(state => state.foo[props.id], [props.id])\nconst foo = useStore(fooSelector)\n```\n\nOr an optional dependencies array to let zustand know when the selector needs to update:\n\n```js\nconst foo = useStore(state => state.foo[props.id], [props.id])\n```\n\nFrom now on your selector is memoized and will only run when either the state changes, or the selector itself.\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## 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\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## Devtools\n\nYes, it's currently [being hashed out](https://github.com/react-spring/zustand/issues/6) but you can already start using it: https://codesandbox.io/s/amazing-kepler-swxol. It works with regular actions as well, you don't need reducers for this.\n","readmeFilename":"readme.md","gitHead":"93683bf5d2ed44a8f1bd69edfeb933c3cb4bb202","_id":"zustand@0.2.2-beta.0","_nodeVersion":"12.4.0","_npmVersion":"6.9.0","dist":{"integrity":"sha512-eQ8i//cCgZyZWdKE9FtRXmxqkgkhJdz/JDtTZ5nGrgUf3sOjTRdDa2MmrKxvIZHFulSzcaTSYNSAcbM2pNblqA==","shasum":"81637bd51129efe66eaeb1e9da5e3f812f1449ca","tarball":"http://registry-npm.ywwl.com/zustand/download/zustand-0.2.2-beta.0.tgz","fileCount":10,"unpackedSize":23043,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdEMrpCRA9TVsSAnZWagAA5v0P/1sci8sgwbdKlyggCOLq\nkFVpk186m8AIMv8DSpmvVZIyXVZhH+EgpL+F8CBE7VQa2guMkYoYESG9FAbX\nE5drObMUXE/nu1nPXA1UUWSCZHXqAqA2x50lsQINS9gReSIOyFODnqlGjLqR\npwr/pL1w9VDsczP8ZEUZIWz/zeVAfgj2f7+8jKnFQXZYlPJg6BcWvOx+m/Tx\n6T3V3pNiWBOXkNyBn9Vk8cyubezc0uXYeLLPCfeT1QJBKsGwUVEUu5Ksjowl\ny0xaPbp+nprhnBGKxRXt/610JwyL0fTbUjVZfV9Nb9BzCM1HscvGHwQAkIxO\nHUfz53taLmZ9pwQYRI2/Xbe1h5kflfztnirHBF1GciPUaz4uWSVxyEKkCuBy\njYivBQhbku8gATswK91NqrNgSYia6N634RRwhVJ4Lb1MDnVTw/Lz6891cpEO\nPHZ8pZHvlnz9+gzN85l+t2eaYQglLKt8k3xMVtgPqT6qE5YYNT3soouCSfso\necys/DksAtL0wjQ2mhaR+RzNCIbWGREcocG0aYefRrrv9EjUJCKThoQd1TkN\nH/Fri0+7b3hWUHaGLMrXKMmvffRANJYOiiRmhyGLtsVkjHDLR5+gFF/d+bhn\n7dg648HXEhfhbu12kXrWasq9fMMlKGs0CwsKG2QbTWHL7S2fwPSBJTWn8y9i\nb59x\r\n=4bpx\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCjXLBu4D96zqyOoZQ01BNaYqYj1ElCxtPTgNjg7Cx2dwIhAPZ8BQK8A5KU3fF8odxgxfiC6Yn95cJv3/7LPbqYIYBs"}],"size":7618,"noattachment":false,"key":"/zustand/-/zustand-0.2.2-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_0.2.2-beta.0_1561381608419_0.9348007446925459"},"_hasShrinkwrap":false,"publish_time":1561381608783,"_cnpm_publish_time":1561381608783,"dist-tags":{"latest":"5.1.0"}}