Repository: Andarist/use-constant Branch: main Commit: 1d9bf0e21014 Files: 11 Total size: 4.3 KB Directory structure: gitextract_nmb8pzja/ ├── .babelrc.js ├── .gitignore ├── .huskyrc ├── .lintstagedrc ├── .npmrc ├── .prettierrc ├── LICENSE ├── README.md ├── package.json ├── src/ │ └── index.ts └── tsconfig.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .babelrc.js ================================================ module.exports = { presets: [ [ '@babel/preset-env', { loose: true, modules: false, }, ], '@babel/preset-typescript', ], } ================================================ FILE: .gitignore ================================================ node_modules dist types ================================================ FILE: .huskyrc ================================================ { "hooks": { "pre-commit": "lint-staged" } } ================================================ FILE: .lintstagedrc ================================================ { "*.{js,ts,json,md}": [ "prettier --write", "git add" ] } ================================================ FILE: .npmrc ================================================ package-lock=false ================================================ FILE: .prettierrc ================================================ { "semi": false, "singleQuote": true, "trailingComma": "all" } ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) Andarist Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # use-constant React hook for creating a value exactly once. `useMemo` doesn't give this guarantee unfortunately - https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily ### Usage Install the package ```bash npm install use-constant # OR yarn add use-constant ``` In your code ```javascript import useConstant from 'use-constant'; const MyComponent = () => { // Give useConstant() a function which should be be executed exactly once and // return in it your constant value const myConstantValue = useConstant(() => 42); // ... ``` ================================================ FILE: package.json ================================================ { "name": "use-constant", "version": "2.0.0", "description": "React hook for creating a value exactly once.", "main": "dist/use-constant.cjs.js", "module": "dist/use-constant.esm.js", "exports": { ".": { "types": { "import": "./dist/use-constant.cjs.mjs", "default": "./dist/use-constant.cjs.js" }, "module": "./dist/use-constant.esm.js", "import": "./dist/use-constant.cjs.mjs", "default": "./dist/use-constant.cjs.js" }, "./package.json": "./package.json" }, "types": "./dist/use-constant.cjs.d.ts", "files": [ "dist", "types" ], "scripts": { "test": "echo \"Warning: no test specified\" || jest --env=node", "build": "preconstruct build", "preversion": "npm test", "prepare": "npm run build" }, "repository": { "type": "git", "url": "git+https://github.com/Andarist/use-constant.git" }, "license": "MIT", "bugs": { "url": "https://github.com/Andarist/use-constant/issues" }, "homepage": "https://github.com/Andarist/use-constant#readme", "peerDependencies": { "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" }, "devDependencies": { "@babel/core": "^7.26.10", "@babel/preset-env": "^7.26.9", "@babel/preset-typescript": "^7.27.0", "@preconstruct/cli": "^2.8.12", "@types/react": "^16.8.8", "fs-extra": "^7.0.1", "husky": "^1.3.1", "jest": "^24.5.0", "lint-staged": "^8.1.5", "prettier": "^1.16.4", "react": "^16.8.4", "typescript": "^5.8.3" }, "preconstruct": { "exports": { "importConditionDefaultExport": "default" }, "___experimentalFlags_WILL_CHANGE_IN_PATCH": { "importsConditions": true } } } ================================================ FILE: src/index.ts ================================================ import * as React from 'react' type ResultBox = { v: T } export default function useConstant(fn: () => T): T { const ref = React.useRef>() if (!ref.current) { ref.current = { v: fn() } } return ref.current.v } ================================================ FILE: tsconfig.json ================================================ { "compilerOptions": { "declaration": true, "declarationDir": "./types", "emitDeclarationOnly": true, "forceConsistentCasingInFileNames": true, "lib": ["esnext"], "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "rootDir": "./src", "skipLibCheck": true, "strict": true, "target": "esnext" } }