Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
RYOSKATE committed May 3, 2020
1 parent 3e5f4de commit a1f4a40
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 13 deletions.
3 changes: 3 additions & 0 deletions esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare type Fn<ARGS extends any[], R> = (...args: ARGS) => R;
declare const useEventCallback: <A extends any[], R>(fn: Fn<A, R>) => Fn<A, R>;
export default useEventCallback;
10 changes: 10 additions & 0 deletions esm/index.js

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

1 change: 1 addition & 0 deletions esm/index.js.map

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

3 changes: 3 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare type Fn<ARGS extends any[], R> = (...args: ARGS) => R;
declare const useEventCallback: <A extends any[], R>(fn: Fn<A, R>) => Fn<A, R>;
export default useEventCallback;
12 changes: 12 additions & 0 deletions lib/index.js

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

1 change: 1 addition & 0 deletions lib/index.js.map

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

20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
"repository": "ssh://git@github.com/WillBooster/react-use-event-callback.git",
"license": "Apache-2.0",
"author": "WillBooster Inc.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "lib/index.js",
"module": "esm/index.js",
"types": "lib/index.d.js",
"files": [
"lib",
"esm"
],
"scripts": {
"build": "tsc",
"build": "yarn build:cjs && yarn build:esm",
"build:cjs": "tsc --build tsconfig.cjs.json",
"build:esm": "tsc --build tsconfig.esm.json",
"cleanup": "yarn format && yarn lint-fix",
"format": "sort-package-json && yarn prettier",
"lint": "eslint \"./{packages/*/,}{src,__tests__}/**/*.{js,jsx,ts,tsx}\"",
Expand All @@ -17,8 +24,11 @@
"typecheck": "tsc --noEmit"
},
"prettier": "@willbooster/prettier-config",
"peerDependencies": {
"react": ">=16.8"
},
"devDependencies": {
"@types/react": "16.9.34",
"@types/react": "^16.8.8",
"@typescript-eslint/eslint-plugin": "2.26.0",
"@typescript-eslint/parser": "2.26.0",
"@willbooster/eslint-config-ts": "3.0.0",
Expand All @@ -31,7 +41,7 @@
"husky": "4.2.3",
"lint-staged": "10.1.1",
"prettier": "2.0.2",
"react": "16.13.1",
"react": "^16.8.4",
"sort-package-json": "1.40.0",
"typescript": "3.8.3"
}
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useCallback, useLayoutEffect, useRef } from 'react';
import { useLayoutEffect, useCallback, useRef } from 'react';

type Fn<ARGS extends any[], R> = (...args: ARGS) => R;

export function useEventCallback<A extends any[], R>(fn: Fn<A, R>): Fn<A, R> {
const ref = useRef<Fn<A, R>>(fn);
const useEventCallback = <A extends any[], R>(fn: Fn<A, R>): Fn<A, R> => {
let ref = useRef<Fn<A, R>>(fn);
useLayoutEffect(() => {
ref.current = fn;
});
return useCallback((...args) => ref.current(...args), []);
}
};

export default useEventCallback;
2 changes: 0 additions & 2 deletions tsconfig.json → tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"alwaysStrict": true,
"strict": true,
Expand All @@ -11,7 +10,6 @@
"resolveJsonModule": true,
"sourceMap": true,
"importHelpers": false,
"outDir": "dist",
"typeRoots": ["./node_modules/@types", "./@types"],
"declaration": true
},
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.base",
"compilerOptions": {
"module": "commonjs",
"outDir": "./lib",
},
"include": ["src/**/*", "__tests__/**/*", "packages/*/src/**/*", "packages/*/__tests__/**/*"]
}
8 changes: 8 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.base",
"compilerOptions": {
"module": "esnext",
"outDir": "./esm",
},
"include": ["src/**/*", "__tests__/**/*", "packages/*/src/**/*", "packages/*/__tests__/**/*"]
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7"
integrity sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw==

"@types/react@16.9.34":
"@types/react@^16.8.8":
version "16.9.34"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.34.tgz#f7d5e331c468f53affed17a8a4d488cd44ea9349"
integrity sha512-8AJlYMOfPe1KGLKyHpflCg5z46n0b5DbRfqDksxBLBTUpB75ypDBAO9eCUcjNwE6LCUslwTz00yyG/X9gaVtow==
Expand Down Expand Up @@ -1739,7 +1739,7 @@ react-is@^16.8.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==

react@16.13.1:
react@^16.8.4:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react/-/react-16.13.1.tgz#2e818822f1a9743122c063d6410d85c1e3afe48e"
integrity sha512-YMZQQq32xHLX0bz5Mnibv1/LHb3Sqzngu7xstSM+vrkE5Kzr9xE0yMByK5kMoTK30YVJE61WfbxIFFvfeDKT1w==
Expand Down

0 comments on commit a1f4a40

Please sign in to comment.