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<T> = { v: T }
export default function useConstant<T>(fn: () => T): T {
const ref = React.useRef<ResultBox<T>>()
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"
}
}
gitextract_nmb8pzja/ ├── .babelrc.js ├── .gitignore ├── .huskyrc ├── .lintstagedrc ├── .npmrc ├── .prettierrc ├── LICENSE ├── README.md ├── package.json ├── src/ │ └── index.ts └── tsconfig.json
SYMBOL INDEX (2 symbols across 1 files)
FILE: src/index.ts
type ResultBox (line 3) | type ResultBox<T> = { v: T }
function useConstant (line 5) | function useConstant<T>(fn: () => T): T {
Condensed preview — 11 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (5K chars).
[
{
"path": ".babelrc.js",
"chars": 173,
"preview": "module.exports = {\n presets: [\n [\n '@babel/preset-env',\n {\n loose: true,\n modules: false,\n "
},
{
"path": ".gitignore",
"chars": 24,
"preview": "node_modules\ndist\ntypes\n"
},
{
"path": ".huskyrc",
"chars": 53,
"preview": "{\n \"hooks\": {\n \"pre-commit\": \"lint-staged\"\n }\n}\n"
},
{
"path": ".lintstagedrc",
"chars": 71,
"preview": "{\n \"*.{js,ts,json,md}\": [\n \"prettier --write\",\n \"git add\"\n ]\n}\n"
},
{
"path": ".npmrc",
"chars": 19,
"preview": "package-lock=false\n"
},
{
"path": ".prettierrc",
"chars": 69,
"preview": "{\n \"semi\": false,\n \"singleQuote\": true,\n \"trailingComma\": \"all\"\n}\n"
},
{
"path": "LICENSE",
"chars": 1060,
"preview": "MIT License\n\nCopyright (c) Andarist\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof thi"
},
{
"path": "README.md",
"chars": 567,
"preview": "# use-constant\n\nReact hook for creating a value exactly once. `useMemo` doesn't give this guarantee unfortunately - http"
},
{
"path": "package.json",
"chars": 1727,
"preview": "{\n \"name\": \"use-constant\",\n \"version\": \"2.0.0\",\n \"description\": \"React hook for creating a value exactly once.\",\n \"m"
},
{
"path": "src/index.ts",
"chars": 245,
"preview": "import * as React from 'react'\n\ntype ResultBox<T> = { v: T }\n\nexport default function useConstant<T>(fn: () => T): T {\n "
},
{
"path": "tsconfig.json",
"chars": 371,
"preview": "{\n \"compilerOptions\": {\n \"declaration\": true,\n \"declarationDir\": \"./types\",\n \"emitDeclarationOnly\": true,\n "
}
]
About this extraction
This page contains the full source code of the Andarist/use-constant GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 11 files (4.3 KB), approximately 1.5k tokens, and a symbol index with 2 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.