-
Notifications
You must be signed in to change notification settings - Fork 47.9k
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
Treat Namespace.use() as a React Hook #15553
Conversation
Hi @jamiebuilds I miss you |
At the time we introduced the plugin internally there were too many false positives from this pattern. So we disabled it. Not that FB code is special — but if we had false positives, it’s likely others will too. I think internal call sites have been removed by now. Maybe we can try again. One particular example I’ve seen is older React Rourer History API. Like Another concern is that we really want to discourage people putting Hooks inside classes. Like |
I think someone would sooner reach for a At this point, React has basically reserved functions called The only place I can see it being annoying is in the intersection of other ecosystems, most notably when server-side rendering React code inside of an express app, express uses
Yeah I'm thinking about this more in the context of factory functions that create objects with hooks on them, like: let Container = createContainer()
function Child() {
let container = Container.useContainer()
// ...
}
function Parent() {
return (
<Container.Provider>
<Child />
<Child />
</Container.Provider>
)
} Someone could fairly easily destructure this, but it seems silly that a lint rule would force them to. @threepointone eat my ass |
Oh yeah |
I think this should also consider how the hook naming is being taught. This is often the definition I hear: I often hear the X part of useX being implied, but taking this definition literally, it means But more importantly, you often hear "any function" which would include namespaces methods. |
I use hooks a lot and have implemented various arbitrary complex custom hooks. Hence I noticed that mocking hooks for my tests might be necessary. But I don't want to mock the whole module only in order to use the important eslint hook rules. Let me introduce another suggestion for namespaced hooks: FooHooks.useBar
Foo.hooks.useBar Following the pattern that the namespace ends with "Hooks" or the accessed hook is stored in some object called "hooks" AND the called function starts with "use". @gaearon How do you think about that? |
This pull request 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. |
bump |
This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated. |
So... We recently made the lint rule stricter, so that it doesn't let you call Hooks from classes. However, we still kept I think I'm fairly sure we don't want to treat We could maybe accept |
So there would be a limit of one hook per namespace? |
No, I meant that let's give |
@gaearon Maybe we can just make this an option for the rule? |
Options undermine conventions. If we think this is a good idea we should probably just do it. |
We merged a more limited version of this in #18722. |
In watching the presentation about building the new facebook.com I saw a reference to a method called:
That was used conditionally, which was confusing because it looked like a hook to me.
I was actually surprised to find out that React does not consider
Namespace.use()
to be a hook at all, and evenNamespace.useHook()
was not considered a hook by the ESLint plugin because it isn'tReact.useHook()
.I want to propose two things:
Namespace.useHook()
should be considered a hook by the ESLint pluginNamespace.use()
should be considered a hook by React/ESLint pluginI've opened this PR with both changes. Although I can easily update it to only include
Namespace.useHook()
if that is preferred.But I would like to argue for also including
Namespace.use()
because the alternative for people wanting to write that isNamespace.useNamespace()
which is just silly to write.