diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..b58b603f --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..24eb271a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..a48ce1d6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/react-use-event-callback.iml b/.idea/react-use-event-callback.iml new file mode 100644 index 00000000..24643cc3 --- /dev/null +++ b/.idea/react-use-event-callback.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 00000000..9b9a4190 --- /dev/null +++ b/.npmignore @@ -0,0 +1,19 @@ +.vscode +**/test/* +.editorconfig +.eslintignore +.eslintrc.json +.gitattributes +.gitignore +.npmignore +.travis.yml +.huskyrc.json +.lintstagedrc.js +prettier.config.js +.prettierignore +renovate.json +.yarnrc +tsconfig.json +tslint.json +package-lock.json +yarn.lock diff --git a/package.json b/package.json index 11212afc..d6e7e1b0 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,14 @@ { "name": "react-use-event-callback", "version": "1.0.0", + "private": false, "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", "scripts": { + "build": "tsc", "cleanup": "yarn format && yarn lint-fix", "format": "sort-package-json && yarn prettier", "lint": "eslint \"./{packages/*/,}{src,__tests__}/**/*.{js,jsx,ts,tsx}\"", @@ -14,6 +18,7 @@ }, "prettier": "@willbooster/prettier-config", "devDependencies": { + "@types/react": "16.9.34", "@typescript-eslint/eslint-plugin": "2.26.0", "@typescript-eslint/parser": "2.26.0", "@willbooster/eslint-config-ts": "3.0.0", @@ -27,6 +32,10 @@ "lint-staged": "10.1.1", "prettier": "2.0.2", "react": "16.13.1", - "sort-package-json": "1.40.0" + "sort-package-json": "1.40.0", + "typescript": "3.8.3" + }, + "peerDependencies": { + "react": ">= 16.8" } } diff --git a/src/index.ts b/src/index.ts index d7d530c7..51f40471 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,18 @@ -import React from 'react'; +import { useCallback, useImperativeHandle, useRef } from 'react'; -export function useEventCallback(): void {} +/** + * Return a memorized callback of the given callback. + * The memorized callback cannot be used in concurrent mode and `useLayoutEffect` of a descendant component. + * When you use the memorized callback in `useLayoutEffect` of a descendant component, + * an error where `ref.current` is `null` occurs. So you can recognize such a bug. + * + * c.f. https://github.com/facebook/react/issues/14099#issuecomment-440013892 + * c.f. https://github.com/facebook/react/issues/14099#issuecomment-499781277 + * c.f. https://github.com/facebook/react/issues/14099#issuecomment-569027159 + * @param callback + */ +export function useEventCallback any>(callback: T): T { + const ref = useRef(callback); + useImperativeHandle(ref, () => callback); + return useCallback((...args: any[]) => (void 0, ref.current)(...args), []) as T; +} diff --git a/tsconfig.json b/tsconfig.json index fc8bc761..5f116630 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,8 @@ "sourceMap": true, "importHelpers": false, "outDir": "dist", - "typeRoots": ["./node_modules/@types", "./@types"] + "typeRoots": ["./node_modules/@types", "./@types"], + "declaration": true }, "include": ["src/**/*", "__tests__/**/*", "packages/*/src/**/*", "packages/*/__tests__/**/*"] } diff --git a/yarn.lock b/yarn.lock index c2524381..11ab999c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -102,6 +102,19 @@ resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA== +"@types/prop-types@*": + version "15.7.3" + 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": + version "16.9.34" + resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.34.tgz#f7d5e331c468f53affed17a8a4d488cd44ea9349" + integrity sha512-8AJlYMOfPe1KGLKyHpflCg5z46n0b5DbRfqDksxBLBTUpB75ypDBAO9eCUcjNwE6LCUslwTz00yyG/X9gaVtow== + dependencies: + "@types/prop-types" "*" + csstype "^2.2.0" + "@typescript-eslint/eslint-plugin@2.26.0": version "2.26.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.26.0.tgz#04c96560c8981421e5a9caad8394192363cc423f" @@ -442,6 +455,11 @@ cross-spawn@^7.0.0: shebang-command "^2.0.0" which "^2.0.1" +csstype@^2.2.0: + version "2.6.10" + resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.10.tgz#e63af50e66d7c266edb6b32909cfd0aabe03928b" + integrity sha512-D34BqZU4cIlMCY93rZHbrq9pjTAQJ3U8S8rfBqjwHxkGPThWFjzZDQpgMJY0QViLxth6ZKYiwFBo14RdN44U/w== + date-fns@^1.27.2: version "1.30.1" resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c" @@ -2155,6 +2173,11 @@ type-fest@^0.8.1: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d" integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA== +typescript@3.8.3: + version "3.8.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061" + integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w== + uri-js@^4.2.2: version "4.2.2" resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.2.2.tgz#94c540e1ff772956e2299507c010aea6c8838eb0"