question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Eslint hooks returned by factory functions not linted

See original GitHub issue

React version: *

Steps To Reproduce

See my draft PR with failing tests 👉 #25066.

Given the following code:

// Factory function for creating hooks
function createHooks() {
  return {
    foo: {
      useQuery: () => {
        data: 'foo.useQuery'
      },
    }
  };
}
const hooks = createHooks();

export const MyComponent = () => {
  if (Math.random() < 0.5) {
    return null;
  }
  // ❌ This should fail the linter
  const query = hooks.foo.useQuery();

  return <>{query.data}</>;
}

The current behavior

The linting does not catch that the hooks.foo.useQuery() is used conditionally.

The expected behavior

The linting should catch that the hooks.foo.useQuery() is used conditionally.

Failing tests / link to code

See my draft PR with failing tests 👉 #25066.

I’ve highlighted areas and things that are up for discussions around this.

Additional context

Partial workaround

If the object returned is not a deep getter, it’s possible to PascalCase it and do it like this:


// Factory function for creating hooks
function createHooks() {
  return {
    useFoo: () => {
      data: 'foo.useQuery'
  };
}
const Hooks = createHooks();

export const MyComponent = () => {
  if (Math.random() < 0.5) {
    return null;
  }
  // ✅ This will fail the linter
  const query = Hooks.useFoo();

  return <>{query.data}</>;
}

It’s okay if hooks can be called outside of React-components

https://github.com/facebook/react/issues/25065#issuecomment-1242767909

Background

I’m the creator of tRPC where we use the following pattern for users to create the root hooks:

// Initialization of the typesafe tRPC hooks
export const trpc = createReactQueryHooks<AppRouter>();

// MyComponent.tsx
export function MyComponent() {
  const query = trpc.useQuery(['post.byId', { id: '1' }])

  return <pre>{JSON.stringify(query.data ?? null, null, 4)}</pre>
}

In the coming version of tRPC, we are planning on have an API that looks like the below, which also won’t be caught.

export function MyComponent() {
  const query = trpc.post.byId.useQury({ id: '1'});

  return <pre>{JSON.stringify(query.data ?? null, null, 4)}</pre>
}

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:10
  • Comments:9

github_iconTop GitHub Comments

5reactions
KATTcommented, Aug 19, 2022

We’re very happy to do a PR to change the behavior to suit our needs, but before doing so I’d love some takes & constraints from the React team so we don’t do a bunch of work in vain that might be rejected regardless.

0reactions
KATTcommented, Nov 4, 2022

Sorry, I dropped the ball on this until someone reminded me that this was not solved.

Personally: Yes, because it would in 100% of the cases produce a runtime error that would pop up during development. It’s nice to lint for that, but it’s easy to detect and easy to fix.

If @KATT is ok with that then I would suggest updating the issue description so that we’re all on the same page.

Yes, this would be fine for me too. I’ve updated the description.


Is there anything we can do to enable this behaviour? Would you accept a PR?

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-plugin-react
Fast, reliable, and secure dependency management.
Read more >
func-names - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
axios-hooks
Start using axios-hooks in your project by running `npm i axios-hooks`. There are 60 other projects in the npm registry using axios-hooks.
Read more >
typeerror: node.params is not iterable
I get TypeError: node.params is not iterable with eslint command using ... \eslint-plugin-react-hooks\index.js) +9ms eslintrc:config-array-factory Plugin .
Read more >
eslint/eslint - Gitter
Get the following error 148:17 error Expected method shorthand object-shorthand . ... (The package "eslint-plugin-react-hook" was not found when loaded as a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found