-
-
Notifications
You must be signed in to change notification settings - Fork 369
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
consistent-function-scoping
: Ignore within useEffect
in React Hooks
#392
Comments
Hm, this is an interesting exception to the rule. Things like hooks or listeners might not have any references to the parent scope. |
Is it definitely better? I imagine Can you provide a more verbose (closer to real life) example to make this clearer? |
It's recommended per the React docs, at least. |
Now that's a different case, they have EDIT: Oh wait, I guess the rule would report import React, { useState, useEffect } from 'react';
function FriendStatus(props) {
const [isOnline, setIsOnline] = useState(null);
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline);
}
ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
// Specify how to clean up after this effect:
return function cleanup() {
ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);
};
});
if (isOnline === null) {
return 'Loading...';
}
return isOnline ? 'Online' : 'Offline';
} |
useEffect(() => {
async function getItems() {
function foo() {}
}
getItems()
}, []) What do we do with function
|
@fisker I believe 2 makes the most sense here, unless I'm missing something |
This doesn't seem to work when using the default import of react: |
In React Hooks, I got:
And it's definitely better to keep the function in the
useEffect
, to group it together. But this rule recommends moving out the function, of course.Perhaps perform a check if the function is in
useEffect
, to ignore it?The text was updated successfully, but these errors were encountered: