Skip to content

Commit

Permalink
eslint config with first hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bgonp committed Jan 27, 2021
1 parent 0c4d83a commit ac01c5e
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 101 deletions.
103 changes: 2 additions & 101 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,104 +1,5 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.vscode/
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
build
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__tests__
12 changes: 12 additions & 0 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require("esbuild")
.build({
entryPoints: ["src/index.ts"],
bundle: true,
minify: true,
outfile: "build/index.js",
target: ["chrome58", "firefox57", "safari11", "edge16"],
define: {
"process.env.NODE_ENV": '"production"',
},
})
.catch(() => process.exit(1));
91 changes: 91 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "custom-hooks",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"prepare": "npm run build",
"build": "node esbuild.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/bgonp/custom-hooks.git"
},
"author": "",
"license": "See LICENSE file",
"bugs": {
"url": "https://github.com/bgonp/custom-hooks/issues"
},
"homepage": "https://github.com/bgonp/custom-hooks#readme",
"dependencies": {
"react": "~17.0.1",
"react-dom": "~17.0.1"
},
"devDependencies": {
"@types/react": "~17.0.0",
"@types/react-dom": "~17.0.0",
"esbuild": "~0.8.36"
}
}
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useLocalStorage } from "./useLocalStorage";

if (typeof window === "undefined") {
throw new Error("bgon/custom-hooks package can only be executed on browser");
}

export default {
useLocalStorage,
};
38 changes: 38 additions & 0 deletions src/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useCallback, useEffect, useState } from 'react'

type useLocalStorageType<T> = [T, (value: T | ((state: T) => T)) => void, () => void]

export const useLocalStorage = <T>(
key: string,
initialValue: T
): useLocalStorageType<T> => {
const [value, setValue] = useState<T>(
(): T => {
try {
const value = window.localStorage.getItem(key)
return value ? JSON.parse(value) : initialValue
} catch (error) {
console.error(error)
return initialValue
}
}
)

const cleanStorage = useCallback<() => void>(() => {
try {
window.localStorage.removeItem(key)
} catch (error) {
console.error(error)
}
}, [key])

useEffect(() => {
try {
window.localStorage.setItem(key, JSON.stringify(value))
} catch (error) {
console.error(error)
}
}, [key, value])

return [value, setValue, cleanStorage]
}

0 comments on commit ac01c5e

Please sign in to comment.