{"name":"use-context-selector","description":"React useContextSelector hook in userland","version":"1.1.4-beta.2","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":"a5aa035db2d84cc7c9509780ce1eb2e1213294e9","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.2","_nodeVersion":"12.18.3","_npmVersion":"6.14.6","dist":{"integrity":"sha512-vDDqicyGN0KcAcyI2PuX/fPE723s9Ys1Zp1jry/4c0atjSFWWDjqLb64ONZBMRbvOjh8YgfzhzCf2DM5sZFm2g==","shasum":"e0962ea6ef0ede523f279e7c8383e79775ebb009","tarball":"http://registry-npm.ywwl.com/use-context-selector/download/use-context-selector-1.1.4-beta.2.tgz","fileCount":10,"unpackedSize":30242,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfaEnrCRA9TVsSAnZWagAAFhgP/jHv2tknFxYiapV2r5oJ\nXPs8R9OKJnDbelJn/WTqVxPlcEvdGKokpdVR93J2M7h5cAUZ5vH0aIZ1P/FD\njBkE4SgLNU8qNsCBs7NSZQzysuDzRrxy68/50+OQKSgQQSfdp5oHpzOVDI9r\nc1IiYFE4GNj99804pWDbnvoh4gEzccAP9c+HmUe0Mv1OAYk3Lm7ukvoo+g/k\nU4QntVQlQbHr2P1B2qqAmLa/U+CI/St9rjrzBbWBe28Xv9igbVgqVuPlCfci\n5z5CeCpYrCrQHfKh6Czp8q45DEScDsZmmEittQGlYigU9jJQzTT5OAH10pDN\n/PcPewKZa1VWyL5fuKSNyXgzoms1gdbla4vnS2o1wWyoR4ywQbW+SwXLnjJ8\nPkHRjhpPxhOU4Js8MkrOpTIEdW93wcB52X9Bs/LIlPY7f1XAnJSzT2VKv6lz\nljRXDwXxLBRoLDLsGBrpyfsS4XljhvAUEQzpDN30sDEtNbThsUnDuIuFKUso\n4V+jyNXxLwbRC5zUHEINvn4gn4beGtSNudDkyBH8EVu0e2FlZVNK1cazGNW1\nrbT/myWd2Ttic6jwB1WiJwf9obD6G/39vJA0Tcw27O1db9Njqkc5/07K3bqj\n/0UgUK4aRYpe5yNDspaw6HDPGfyLGtOFRJLihuiH2EXbExV3S5qZ6fEmNbwn\n+Rhz\r\n=fmKp\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGM5AsF0i28ypubh2KDkPAkVdW7SgUwBBWdDFHcJXATGAiEApd6JpNvhcmWjWrw//EZYpRED4Ya/JdSwCgPN+SpWchw="}],"size":8347,"noattachment":false,"key":"/use-context-selector/-/use-context-selector-1.1.4-beta.2.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.2_1600670186611_0.5793301320954969"},"_hasShrinkwrap":false,"publish_time":1600670186800,"_cnpm_publish_time":1600670186800,"dist-tags":{"latest":"2.0.0","next":"2.0.0-beta.0"}}