Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about incorrect use of useImperativeHandle() #14647

Merged
merged 1 commit into from
Jan 21, 2019

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Jan 21, 2019

Fixes #14629.
I've made these mistakes quite a few times and I expect more people to do it.

This protects against:

  • useImperativeHandle({ focus })
  • useImperativeHandle(ref, { focus })

(Correct one is useImperativeHandle(ref, () => ({ focus })).)

While adding these warnings I also noticed we don't set current fiber during some effect traversals. This seems like a regression. It caused warnings from Hooks during commit phase to have no component stack. So I fixed that too.

warning(
refObject.hasOwnProperty('current'),
'Expected useImperativeHandle() first argument to either be a ' +
'ref callback or React.createRef() object. Instead received: %s.',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Note: callback is handled by previous if-clause so we don't need to check for that)

@sizebot
Copy link

sizebot commented Jan 21, 2019

ReactDOM: size: 0.0%, gzip: 0.0%

Details of bundled changes.

Comparing: b5a3df6...64841ee

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.1% +0.1% 734.18 KB 734.83 KB 169.19 KB 169.37 KB UMD_DEV
react-dom.production.min.js 0.0% 0.0% 98.98 KB 98.98 KB 32.2 KB 32.2 KB UMD_PROD
react-dom.profiling.min.js 0.0% 0.0% 101.97 KB 101.97 KB 32.84 KB 32.84 KB UMD_PROFILING
react-dom.development.js +0.1% +0.1% 729.24 KB 729.89 KB 167.77 KB 167.93 KB NODE_DEV
react-dom.production.min.js 0.0% 0.0% 98.99 KB 98.99 KB 31.68 KB 31.68 KB NODE_PROD
react-dom.profiling.min.js 0.0% 0.0% 102.09 KB 102.09 KB 32.31 KB 32.31 KB NODE_PROFILING
ReactDOM-dev.js +0.1% +0.1% 751.62 KB 752.43 KB 169.05 KB 169.27 KB FB_WWW_DEV
ReactDOM-profiling.js 0.0% 0.0% 318.68 KB 318.68 KB 58.83 KB 58.83 KB FB_WWW_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 734.52 KB 735.18 KB 169.34 KB 169.51 KB UMD_DEV
react-dom-unstable-fire.production.min.js 0.0% 0.0% 99 KB 99 KB 32.21 KB 32.21 KB UMD_PROD
react-dom-unstable-fire.profiling.min.js 0.0% 0.0% 101.98 KB 101.98 KB 32.85 KB 32.85 KB UMD_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 729.58 KB 730.24 KB 167.91 KB 168.08 KB NODE_DEV
react-dom-unstable-fire.production.min.js 0.0% 0.0% 99 KB 99 KB 31.69 KB 31.69 KB NODE_PROD
react-dom-unstable-fire.profiling.min.js 0.0% 0.0% 102.1 KB 102.1 KB 32.31 KB 32.32 KB NODE_PROFILING
ReactFire-dev.js +0.1% +0.1% 750.83 KB 751.64 KB 168.99 KB 169.22 KB FB_WWW_DEV
ReactFire-profiling.js 0.0% 0.0% 307.2 KB 307.2 KB 56.51 KB 56.51 KB FB_WWW_PROFILING
react-dom-test-utils.development.js 0.0% 0.0% 44.87 KB 44.87 KB 12.3 KB 12.3 KB UMD_DEV
react-dom-test-utils.development.js 0.0% 0.0% 44.59 KB 44.59 KB 12.24 KB 12.24 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% 0.0% 9.74 KB 9.74 KB 3.65 KB 3.65 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 60.61 KB 60.61 KB 15.92 KB 15.92 KB UMD_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% 🔺+0.1% 11.01 KB 11.01 KB 3.81 KB 3.81 KB UMD_PROD
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 60.29 KB 60.29 KB 15.79 KB 15.79 KB NODE_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% 🔺+0.1% 10.75 KB 10.75 KB 3.71 KB 3.71 KB NODE_PROD
react-dom-server.browser.development.js 0.0% 0.0% 125.09 KB 125.09 KB 33.35 KB 33.35 KB UMD_DEV
react-dom-server.browser.production.min.js 0.0% 0.0% 16.96 KB 16.96 KB 6.53 KB 6.53 KB UMD_PROD
react-dom-server.browser.development.js 0.0% 0.0% 121.22 KB 121.22 KB 32.43 KB 32.43 KB NODE_DEV
react-dom-server.browser.production.min.js 0.0% 0.0% 16.87 KB 16.87 KB 6.51 KB 6.51 KB NODE_PROD
ReactDOMServer-dev.js 0.0% 0.0% 122.4 KB 122.4 KB 32.07 KB 32.07 KB FB_WWW_DEV
ReactDOMServer-prod.js 0.0% 0.0% 45.18 KB 45.18 KB 10.39 KB 10.4 KB FB_WWW_PROD
react-dom-server.node.development.js 0.0% 0.0% 123.28 KB 123.28 KB 32.97 KB 32.97 KB NODE_DEV
react-dom-server.node.production.min.js 0.0% 0.0% 17.74 KB 17.74 KB 6.83 KB 6.83 KB NODE_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% 🔺+0.1% 1.21 KB 1.21 KB 705 B 706 B UMD_PROD
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.45 KB 3.45 KB 1.39 KB 1.39 KB NODE_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% 🔺+0.2% 1.05 KB 1.05 KB 636 B 637 B NODE_PROD
react-dom-unstable-fizz.node.production.min.js 0.0% 🔺+0.2% 1.1 KB 1.1 KB 666 B 667 B NODE_PROD

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.1% +0.2% 513.02 KB 513.68 KB 113.02 KB 113.2 KB UMD_DEV
react-art.production.min.js 0.0% 0.0% 91.04 KB 91.04 KB 27.97 KB 27.97 KB UMD_PROD
react-art.development.js +0.1% +0.2% 444.55 KB 445.2 KB 95.92 KB 96.08 KB NODE_DEV
react-art.production.min.js 0.0% 0.0% 56.01 KB 56.01 KB 17.25 KB 17.25 KB NODE_PROD
ReactART-dev.js +0.2% +0.2% 453.34 KB 454.15 KB 95.08 KB 95.31 KB FB_WWW_DEV

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-dev.js +0.1% +0.2% 577.81 KB 578.61 KB 125.59 KB 125.82 KB RN_FB_DEV
ReactNativeRenderer-dev.js +0.1% +0.2% 577.72 KB 578.53 KB 125.55 KB 125.79 KB RN_OSS_DEV
ReactNativeRenderer-profiling.js 0.0% 0.0% 233.31 KB 233.31 KB 40.91 KB 40.92 KB RN_OSS_PROFILING
ReactFabric-dev.js +0.1% +0.2% 568.66 KB 569.47 KB 123.31 KB 123.54 KB RN_FB_DEV
ReactFabric-prod.js 0.0% 0.0% 236.22 KB 236.22 KB 41.29 KB 41.29 KB RN_FB_PROD
ReactFabric-profiling.js 0.0% 0.0% 241.52 KB 241.52 KB 42.64 KB 42.65 KB RN_FB_PROFILING
ReactFabric-dev.js +0.1% +0.2% 568.57 KB 569.37 KB 123.26 KB 123.5 KB RN_OSS_DEV
ReactFabric-prod.js 0.0% 0.0% 220.53 KB 220.53 KB 38.07 KB 38.07 KB RN_OSS_PROD

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +0.1% +0.2% 457.76 KB 458.42 KB 98.68 KB 98.84 KB UMD_DEV
react-test-renderer.production.min.js 0.0% 0.0% 57.41 KB 57.41 KB 17.62 KB 17.63 KB UMD_PROD
react-test-renderer.development.js +0.1% +0.2% 452.72 KB 453.38 KB 97.44 KB 97.61 KB NODE_DEV
react-test-renderer.production.min.js 0.0% 0.0% 57.08 KB 57.08 KB 17.47 KB 17.47 KB NODE_PROD
ReactTestRenderer-dev.js +0.2% +0.2% 461.73 KB 462.54 KB 96.97 KB 97.2 KB FB_WWW_DEV
react-test-renderer-shallow.development.js 0.0% 0.0% 38.09 KB 38.09 KB 9.83 KB 9.83 KB UMD_DEV
react-test-renderer-shallow.production.min.js 0.0% 0.0% 10.56 KB 10.56 KB 3.25 KB 3.25 KB UMD_PROD
react-test-renderer-shallow.development.js 0.0% 0.0% 32.39 KB 32.39 KB 8.46 KB 8.46 KB NODE_DEV
react-test-renderer-shallow.production.min.js 0.0% 0.0% 11.2 KB 11.2 KB 3.49 KB 3.49 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js +0.1% +0.2% 442.38 KB 443.03 KB 94.43 KB 94.59 KB NODE_DEV
react-reconciler.production.min.js 0.0% 0.0% 57.17 KB 57.17 KB 17.07 KB 17.07 KB NODE_PROD
react-reconciler-persistent.development.js +0.1% +0.2% 440.75 KB 441.41 KB 93.78 KB 93.94 KB NODE_DEV
react-reconciler-reflection.development.js 0.0% 0.0% 15.4 KB 15.4 KB 4.83 KB 4.83 KB NODE_DEV
react-reconciler-reflection.production.min.js 0.0% 🔺+0.1% 2.56 KB 2.56 KB 1.13 KB 1.13 KB NODE_PROD

Generated by 🚫 dangerJS

@gaearon gaearon merged commit 8f45a7f into facebook:master Jan 21, 2019
@gaearon gaearon deleted the ref-warn-hook branch January 21, 2019 19:56
gaearon added a commit that referenced this pull request Jan 21, 2019
gaearon added a commit that referenced this pull request Jan 21, 2019
* Revert "Add test coverage for readContext() on the server (#14649)"

This reverts commit fe2ecd2.

* Revert "Warn about incorrect use of useImperativeHandle() (#14647)"

This reverts commit 8f45a7f.

* Revert "Disallow reading context during useMemo etc (#14648)"

This reverts commit 1fcbd22.
gaearon added a commit that referenced this pull request Jan 21, 2019
gaearon added a commit that referenced this pull request Jan 21, 2019
…Mode" (#14652)

* Revert "Revert "Disallow reading context during useMemo etc" (#14651)"

This reverts commit 5fce648.

* Revert "Add test coverage for readContext() on the server (#14649)"

This reverts commit fe2ecd2.

* Revert "Warn about incorrect use of useImperativeHandle() (#14647)"

This reverts commit 8f45a7f.

* Revert "Disallow reading context during useMemo etc (#14648)"

This reverts commit 1fcbd22.

* Revert "Warn about refs on lazy function components (#14645)"

This reverts commit 2a084f5.

* Revert "Fix typo (#14560)"

This reverts commit b5a3df6.

* Revert "fix typo (#14316)"

This reverts commit 9c146e6.

* Revert "Mention forwardRef() in <Fn ref={...} /> errors and warnings (#14644)"

This reverts commit baa6d40.

* Revert "Double-render function components with Hooks in DEV in StrictMode (#14643)"

This reverts commit a1414e8.
jetoneza pushed a commit to jetoneza/react that referenced this pull request Jan 23, 2019
jetoneza pushed a commit to jetoneza/react that referenced this pull request Jan 23, 2019
* Revert "Add test coverage for readContext() on the server (facebook#14649)"

This reverts commit fe2ecd2.

* Revert "Warn about incorrect use of useImperativeHandle() (facebook#14647)"

This reverts commit 8f45a7f.

* Revert "Disallow reading context during useMemo etc (facebook#14648)"

This reverts commit 1fcbd22.
jetoneza pushed a commit to jetoneza/react that referenced this pull request Jan 23, 2019
…Mode" (facebook#14652)

* Revert "Revert "Disallow reading context during useMemo etc" (facebook#14651)"

This reverts commit 5fce648.

* Revert "Add test coverage for readContext() on the server (facebook#14649)"

This reverts commit fe2ecd2.

* Revert "Warn about incorrect use of useImperativeHandle() (facebook#14647)"

This reverts commit 8f45a7f.

* Revert "Disallow reading context during useMemo etc (facebook#14648)"

This reverts commit 1fcbd22.

* Revert "Warn about refs on lazy function components (facebook#14645)"

This reverts commit 2a084f5.

* Revert "Fix typo (facebook#14560)"

This reverts commit b5a3df6.

* Revert "fix typo (facebook#14316)"

This reverts commit 9c146e6.

* Revert "Mention forwardRef() in <Fn ref={...} /> errors and warnings (facebook#14644)"

This reverts commit baa6d40.

* Revert "Double-render function components with Hooks in DEV in StrictMode (facebook#14643)"

This reverts commit a1414e8.
n8schloss pushed a commit to n8schloss/react that referenced this pull request Jan 31, 2019
n8schloss pushed a commit to n8schloss/react that referenced this pull request Jan 31, 2019
* Revert "Add test coverage for readContext() on the server (facebook#14649)"

This reverts commit fe2ecd2.

* Revert "Warn about incorrect use of useImperativeHandle() (facebook#14647)"

This reverts commit 8f45a7f.

* Revert "Disallow reading context during useMemo etc (facebook#14648)"

This reverts commit 1fcbd22.
n8schloss pushed a commit to n8schloss/react that referenced this pull request Jan 31, 2019
…Mode" (facebook#14652)

* Revert "Revert "Disallow reading context during useMemo etc" (facebook#14651)"

This reverts commit 5fce648.

* Revert "Add test coverage for readContext() on the server (facebook#14649)"

This reverts commit fe2ecd2.

* Revert "Warn about incorrect use of useImperativeHandle() (facebook#14647)"

This reverts commit 8f45a7f.

* Revert "Disallow reading context during useMemo etc (facebook#14648)"

This reverts commit 1fcbd22.

* Revert "Warn about refs on lazy function components (facebook#14645)"

This reverts commit 2a084f5.

* Revert "Fix typo (facebook#14560)"

This reverts commit b5a3df6.

* Revert "fix typo (facebook#14316)"

This reverts commit 9c146e6.

* Revert "Mention forwardRef() in <Fn ref={...} /> errors and warnings (facebook#14644)"

This reverts commit baa6d40.

* Revert "Double-render function components with Hooks in DEV in StrictMode (facebook#14643)"

This reverts commit a1414e8.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useImperativeHandle should warn when second arg isn't a function
4 participants