-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
Bug: eslint-plugin-react-hooks does not allow passing hook as props in jsx syntax #24108
Comments
Why would you expect this to be allowed? It is explicitly stated that you can only use hooks in function components or custom hooks. |
Well this is not breaking rules of hooks , here we are passing hook to a component and If the second syntax is allowed, why isn't the first syntax allowed as well? |
Hey @alisajadih, the documentation explains that we should use hooks only at the top level
The example you sent is using the naming convention that identifies your function as a hook (with const useCustomeHook = () => {
return <div />;
}; Possible solution👉🏻 If you rename your Hooks' purposeI guess this question came because the purpose of the hook is a bit confuse for you. I'd check the Introducing Hooks documentation, to make sure you can clarify this point. In summary, hooks are useful to deal with component lifecycle when it is just a simple function rather than a class with inheritance and predefined functions. From the documentation:
|
Thanks for your answers. I know rules of hooks and hooks aspects. I want to make an inline custom hook (by passing anonymouse function as const useCostomHook = () => {/*do something*/}
<IsolateRender useRender={useCustomeHook} /> |
I think the misunderstanding here is 1) defining a custom hook and passing that as a prop which is then later invoked at the top level of a component and thus follows the rules of hooks vs 2) calling the hook when passing it as a prop. The original code does 1), not 2) and so doesn't violate the rules of hooks. I was just looking into this and it seems it's covered in the docs now: https://react.dev/reference/rules/react-calls-components-and-hooks#dont-dynamically-use-hooks
function ChatInput() {
return <Button useData={useDataWithLogging} /> // 🔴 Bad: don't pass hooks as props
}
function ChatInput() {
return <Button />
}
function Button() {
const data = useDataWithLogging(); // ✅ Good: Use the hook directly
}
function useDataWithLogging() {
// If there's any conditional logic to change the hook's behavior, it should be inlined into
// the hook
}
In my opinion though, this section of the docs is too strong in its stance and doesn't offer any real alternative. Simply moving the hook down in many cases doesn't solve what you were probably trying to achieve. I don't see the problem if you name the prop starting with "use" and pass it down because then I'd expect the eslint plugin to catch the improper usage of the hook prop so the following statement would be wrong:
I think the 2 main arguments are "local reasoning" which can depends on context, and the de-optimization it mentions React does. I'd say if the error would be kept because these are useful to know in general, then updating the error message to reflect the correct reason would be a good approach. I could try to pick this up if the maintainers agree. |
Just did a bit of digging into the PR which added this to the docs, and it's more of a React Forget Compiler thing: reactjs/react.dev#6680 (comment)
I would prefer the error message to be more accurate still. Also "hard rule" would mean React will de-opt I suppose, not that you'd have a bug since it's probably just that the react compiler won't be able to automatically memoize. |
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! |
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! |
React version: latest
demo:https://codesandbox.io/s/cranky-framework-p7r5vb?file=/src/App.tsx
The current behavior
jsx syntax does not work correctly.
it shows this error:
The expected behavior
jsx syntax should work as object syntax.
The text was updated successfully, but these errors were encountered: