Skip to content
New issue

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

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

Already on GitHub? # to your account

Better error message when useEffect is called with no parameters #15194

Closed
JoshuaKGoldberg opened this issue Mar 22, 2019 · 5 comments
Closed
Labels
Resolution: Stale Automatically closed due to inactivity Type: Enhancement

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Mar 22, 2019

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

Suppose you give an undefined object or forget to provide parameters to useEffect:

const App = () => {
    React.useEffect();
    return "";
};

ReactDOM.render(<App />, document.getElementById('container'));

On JSFiddle (https://jsfiddle.net/g39javzq), this error stack is logged:

Uncaught TypeError: create is not a function
    at commitHookEffectList (react-dom.development.js:17372)
    at commitPassiveHookEffects (react-dom.development.js:17396)
    at HTMLUnknownElement.callCallback (react-dom.development.js:143)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:193)
    at invokeGuardedCallback (react-dom.development.js:250)
    at commitPassiveEffects (react-dom.development.js:18863)
    at wrapped (react.development.js:1353)
    at flushFirstCallback (react.development.js:587)
    at flushWork (react.development.js:699)
    at MessagePort.channel.port1.onmessage (react.development.js:1093)

...though on a local project in React 16.8.4, this stack is logged:

Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
    at _iterableToArrayLimit (my-project.chunk.js:123)
    at _slicedToArray (my-project.chunk.js:456)
    at MyComponent (myFile.js:7)

What is the expected behavior?

Neither of these were useful during the 5 minutes it took to find the culprit useEffect that should have been a useState.

In development, we check for an undefined deps argument in useEffect and give a better warning?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React 16.8.4, Chrome on Mac.

@gaearon
Copy link
Collaborator

gaearon commented Mar 22, 2019

Sure, want to send a PR?

@stale
Copy link

stale bot commented Jan 10, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Jan 10, 2020
@JoshuaKGoldberg
Copy link
Contributor Author

JoshuaKGoldberg commented Jan 10, 2020

Will do this thing soon! I don't really have the time to look into React development now. Up for grabs!

@stale stale bot removed the Resolution: Stale Automatically closed due to inactivity label Jan 10, 2020
@stale
Copy link

stale bot commented Apr 11, 2020

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Apr 11, 2020
@stale
Copy link

stale bot commented Apr 18, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@stale stale bot closed this as completed Apr 18, 2020
poteto added a commit that referenced this issue Feb 11, 2025
## Summary

Fixes #32354.

Re-creation of #15197: adds a dev-only warning if `create == null` to
the three `use*Effect` functions:

* `useEffect`
* `useInsertionEffect`
* `useLayoutEffect`

Updates the warning to match the same text given in the
`react/exhaustive-deps` lint rule.

## How did you test this change?

I applied the changes manually within `node_modules/` on a local clone
of
https://github.com/JoshuaKGoldberg/repros/tree/react-use-effect-no-arguments.

Please pardon me for opening a PR addressing a not-accepted issue. I was
excited to get back to #15194 -> #15197 now that I have time. 🙂

---------

Co-authored-by: lauren <poteto@users.noreply.github.com>
github-actions bot pushed a commit that referenced this issue Feb 11, 2025
## Summary

Fixes #32354.

Re-creation of #15197: adds a dev-only warning if `create == null` to
the three `use*Effect` functions:

* `useEffect`
* `useInsertionEffect`
* `useLayoutEffect`

Updates the warning to match the same text given in the
`react/exhaustive-deps` lint rule.

## How did you test this change?

I applied the changes manually within `node_modules/` on a local clone
of
https://github.com/JoshuaKGoldberg/repros/tree/react-use-effect-no-arguments.

Please pardon me for opening a PR addressing a not-accepted issue. I was
excited to get back to #15194 -> #15197 now that I have time. 🙂

---------

Co-authored-by: lauren <poteto@users.noreply.github.com>

DiffTrain build for [192555b](192555b)
github-actions bot pushed a commit that referenced this issue Feb 11, 2025
## Summary

Fixes #32354.

Re-creation of #15197: adds a dev-only warning if `create == null` to
the three `use*Effect` functions:

* `useEffect`
* `useInsertionEffect`
* `useLayoutEffect`

Updates the warning to match the same text given in the
`react/exhaustive-deps` lint rule.

## How did you test this change?

I applied the changes manually within `node_modules/` on a local clone
of
https://github.com/JoshuaKGoldberg/repros/tree/react-use-effect-no-arguments.

Please pardon me for opening a PR addressing a not-accepted issue. I was
excited to get back to #15194 -> #15197 now that I have time. 🙂

---------

Co-authored-by: lauren <poteto@users.noreply.github.com>

DiffTrain build for [192555b](192555b)
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Resolution: Stale Automatically closed due to inactivity Type: Enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants