{"name":"use-context-selector","description":"React useContextSelector hook in userland","version":"1.1.4-beta.1","publishConfig":{"tag":"beta"},"author":{"name":"Daishi Kato"},"repository":{"type":"git","url":"git+https://github.com/dai-shi/use-context-selector.git"},"source":"./src/index.js","main":"./dist/index.umd.js","module":"./dist/index.modern.js","types":"./src/index.d.ts","sideEffects":false,"scripts":{"compile":"microbundle build -f modern,umd","test":"run-s eslint tsc-test jest","eslint":"eslint --ext .js,.ts,.tsx --ignore-pattern dist .","jest":"jest","tsc-test":"tsc --project . --noEmit","apidoc":"documentation readme --section API --markdown-toc false src/index.js","examples:01_minimal":"DIR=01_minimal EXT=js webpack-dev-server","examples:02_typescript":"DIR=02_typescript webpack-dev-server"},"keywords":["react","context","hooks"],"license":"MIT","dependencies":{},"devDependencies":{"@babel/cli":"^7.10.5","@babel/core":"^7.11.0","@babel/preset-env":"^7.11.0","@babel/preset-react":"^7.10.4","@testing-library/react":"^10.4.7","@types/react":"^16.9.44","@types/react-dom":"^16.9.8","@typescript-eslint/eslint-plugin":"^3.7.1","@typescript-eslint/parser":"^3.7.1","babel-loader":"^8.1.0","documentation":"^13.0.2","eslint":"^7.6.0","eslint-config-airbnb":"^18.2.0","eslint-plugin-import":"^2.22.0","eslint-plugin-jsx-a11y":"^6.3.1","eslint-plugin-react":"^7.20.5","eslint-plugin-react-hooks":"^4.0.8","html-webpack-plugin":"^4.3.0","jest":"^26.2.2","microbundle":"^0.12.3","npm-run-all":"^4.1.5","react":"experimental","react-dom":"experimental","ts-loader":"^8.0.2","typescript":"^3.9.7","webpack":"^4.44.1","webpack-cli":"^3.3.12","webpack-dev-server":"^3.11.0"},"peerDependencies":{"react":">=16.8.0"},"babel":{"presets":[["@babel/preset-env",{"targets":"> 0.2%, not dead"}],"@babel/preset-react"]},"gitHead":"d6b9fd3a67c931a66c6004a45fd1972719f9858d","readme":"# use-context-selector\n\n[![CI](https://img.shields.io/github/workflow/status/dai-shi/use-context-selector/CI)](https://github.com/dai-shi/use-context-selector/actions?query=workflow%3ACI)\n[![npm](https://img.shields.io/npm/v/use-context-selector)](https://www.npmjs.com/package/use-context-selector)\n[![size](https://img.shields.io/bundlephobia/minzip/use-context-selector)](https://bundlephobia.com/result?p=use-context-selector)\n\nReact useContextSelector hook in userland\n\n## Introduction\n\nReact Context and useContext is often used to avoid prop drilling,\nhowever it's known that there's a performance issue.\nWhen a context value is changed, all components that useContext\nwill re-render.\n\n[useContextSelector](https://github.com/reactjs/rfcs/pull/119) is recently proposed.\nWhile waiting for the process, this library provides the API in userland.\n\n## Install\n\n```bash\nnpm install use-context-selector\n```\n\n## Usage\n\n```javascript\nimport React, { useState } from 'react';\nimport ReactDOM from 'react-dom';\n\nimport { createContext, useContextSelector } from 'use-context-selector';\n\nconst context = createContext(null);\n\nconst Counter1 = () => {\n  const count1 = useContextSelector(context, v => v[0].count1);\n  const setState = useContextSelector(context, v => v[1]);\n  const increment = () => setState(s => ({\n    ...s,\n    count1: s.count1 + 1,\n  }));\n  return (\n    <div>\n      <span>Count1: {count1}</span>\n      <button type=\"button\" onClick={increment}>+1</button>\n      {Math.random()}\n    </div>\n  );\n};\n\nconst Counter2 = () => {\n  const count2 = useContextSelector(context, v => v[0].count2);\n  const setState = useContextSelector(context, v => v[1]);\n  const increment = () => setState(s => ({\n    ...s,\n    count2: s.count2 + 1,\n  }));\n  return (\n    <div>\n      <span>Count2: {count2}</span>\n      <button type=\"button\" onClick={increment}>+1</button>\n      {Math.random()}\n    </div>\n  );\n};\n\nconst StateProvider = ({ children }) => {\n  const [state, setState] = useState({ count1: 0, count2: 0 });\n  return (\n    <context.Provider value={[state, setState]}>\n      {children}\n    </context.Provider>\n  );\n};\n\nconst App = () => (\n  <StateProvider>\n    <Counter1 />\n    <Counter2 />\n  </StateProvider>\n);\n\nReactDOM.render(<App />, document.getElementById('app'));\n```\n\n## Technical memo\n\nReact context by nature triggers propagation of component re-rendering\nif a value is changed. To avoid this, this library uses undocumented\nfeature of `calculateChangedBits`. It then uses a subscription model\nto force update when a component needs to re-render.\n\n## API\n\n<!-- Generated by documentation.js. Update this documentation by updating the source code. -->\n\n### createContext\n\nThis creates a special context for `useContextSelector`.\n\n#### Parameters\n\n-   `defaultValue` **any** \n\n#### Examples\n\n```javascript\nconst PersonContext = createContext({ firstName: '', familyName: '' });\n```\n\nReturns **React.Context** \n\n### useContextSelector\n\nThis hook returns context selected value by selector.\nIt will only accept context created by `createContext`.\nIt will trigger re-render if only the selected value is referentially changed.\n\n#### Parameters\n\n-   `context` **React.Context** \n-   `selector` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** \n\n#### Examples\n\n```javascript\nconst firstName = useContextSelector(PersonContext, state => state.firstName);\n```\n\nReturns **any** \n\n### useContext\n\nThis hook returns the entire context value.\nUse this instead of React.useContext for consistent behavior.\n\n#### Parameters\n\n-   `context` **React.Context** \n\n#### Examples\n\n```javascript\nconst person = useContext(PersonContext);\n```\n\nReturns **any** \n\n### BridgeProvider\n\nThis is a Provider component for bridging multiple react roots\n\n#### Parameters\n\n-   `props` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** \n    -   `props.context` **React.Context** \n    -   `props.value` **any** \n    -   `props.children` **React.ReactNote** \n\n#### Examples\n\n```javascript\nconst valueToBridge = useContext(PersonContext);\nreturn (\n  <Renderer>\n    <BridgeProvider context={PersonContext} value={valueToBridge}>\n      {children}\n    </Bidge>\n  </Renderer>\n);\n```\n\nReturns **React.ReactElement** \n\n## Limitations\n\n-   Subscriptions are per-context basis. So, even if there are multiple context providers in a component tree, all components are subscribed to all providers. This may lead false positives (extra re-renders).\n-   In order to stop propagation, `children` of a context provider has to be either created outside of the provider or memoized with `React.memo`.\n-   Provider trigger re-renders only if the context value is referentially changed.\n-   Context consumers are not supported.\n-   The [stale props](https://react-redux.js.org/api/hooks#stale-props-and-zombie-children) issue can't be solved in userland. (workaround with try-catch)\n\n## Examples\n\nThe [examples](examples) folder contains working examples.\nYou can run one of them with\n\n```bash\nPORT=8080 npm run examples:01_minimal\n```\n\nand open <http://localhost:8080> in your web browser.\n\nYou can also try them in codesandbox.io:\n[01](https://codesandbox.io/s/github/dai-shi/use-context-selector/tree/master/examples/01_minimal)\n[02](https://codesandbox.io/s/github/dai-shi/use-context-selector/tree/master/examples/02_typescript)\n\n## Related projects\n\n-   [react-tracked](https://github.com/dai-shi/react-tracked)\n-   [reactive-react-redux](https://github.com/dai-shi/reactive-react-redux)\n","readmeFilename":"README.md","bugs":{"url":"https://github.com/dai-shi/use-context-selector/issues"},"homepage":"https://github.com/dai-shi/use-context-selector#readme","_id":"use-context-selector@1.1.4-beta.1","_nodeVersion":"12.18.3","_npmVersion":"6.14.6","dist":{"integrity":"sha512-jT/7CgfEA/Zg83CmO85EwZdNRUlBTWfn+mP28GWTx1IAgYPcbIhVfSKA9wgDyowXQQGQkuu++gV7/bpNSDXV/Q==","shasum":"905c7401b17f048eb646891fa3e770bd2accc2b0","tarball":"http://registry-npm.ywwl.com/use-context-selector/download/use-context-selector-1.1.4-beta.1.tgz","fileCount":10,"unpackedSize":32366,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfaDxECRA9TVsSAnZWagAAIcYP+wTetw3sia1s9taJ+Hle\n2nsi+e2rcWeM7P6sk57/U9XIa9AgpTOvcEEKJuCzk+MUazC55hVTJ7SzwvSw\nGL/0Hd7T7Rg1gkhK3v7qydIQUuwsx+lLwKuR9rScSHmmzIW4OUMTAYFO+JwH\nfCrppK8MUYM3x87Cvor7jyF2kBZ/EKkLrXZcTfS31s1sBQj/f1FAMOwgENk+\n+WtJtTXvb//mtZqRjqLsflt3WGOQGhVSj5WtrlWCCqx04G3GiZD9lfyTcuzV\nTjGuVwvRqaUPRWF27252i/BJgmypYjVnr9SYiX18TP3YyHDKBrQXAFnhqoxC\nSErvP76VEGwd1w8KPvzEeyPducE8wa6Syr0tl1C1a6+YjbKIa3rj53V5+EKG\nuxv1qjzKYNlcHKhIJb/A96CvkHJ7ED781hjkhbKVmHP/YlQPri9n8usGEwSH\npttN8YzFYvUyHKO6ytAvUKmSczTXQV38mXprFwJSnr4fpLT4U79tHIzTtO4o\nWAd84z4kchihTA5m0skWlSxuAlBleh4PrqUbZzOdofU+jdjTKcspYm+bTuJP\nrfu0jMcP6y6nEJc508hPwXnzV0pp5UvU/Kw/gp8ZwiG9KUhVE/2ZMVsup13x\nSGt1LFOXzfrmt/nsRRp59Kt/gbml7kbhVMFy0Smk1RFJC0EfQDnV9hWWDdki\nDzfq\r\n=QSZ1\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCpKS9R6MdT6qYwcw50Xp3HPYwNCQTN+szzjcpsMc+PbQIgYKoMI05PTZbyMi/G48eSvHzCOzkfFIrfdRcGkdvTNVg="}],"size":8438,"noattachment":false,"key":"/use-context-selector/-/use-context-selector-1.1.4-beta.1.tgz"},"maintainers":[{"name":"daishi","email":"daishi@axlight.com"}],"_npmUser":{"name":"daishi","email":"daishi@axlight.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/use-context-selector_1.1.4-beta.1_1600666692367_0.8894145319214171"},"_hasShrinkwrap":false,"publish_time":1600666692501,"_cnpm_publish_time":1600666692501,"dist-tags":{"latest":"2.0.0","next":"2.0.0-beta.0"}}