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.

Cannot use hooks because of how containers call their views

See original GitHub issue

The following:

const MyResultView = () => {
  const someHook = useHook();

  return <div>My Result</div>;
};

<Result view={MyResultView} />

Errors with: https://reactjs.org/warnings/invalid-hook-call-warning.html

I think this is because of how containers call their views:

https://github.com/elastic/search-ui/blob/c68f78f9f44680f8219c8aa1e2ed5442c56936af/packages/react-search-ui/src/containers/Result.js#L43-L50

When I change that to:

    return <View
      className={className},
      result={result}
      key={`result-${result.id.raw}`}
      onClickLink={() => this.handleClickLink(result.id.raw)}
      titleField={titleField}
      urlField={urlField}
    />;

Everything works fine.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
artemis-primecommented, May 18, 2020

Jason, re “I’m hesitant to make the switch from View() to <View /> because I don’t fully understand the functional differences between the two styles.” … the difference is that one is seriously non-idiomatic to React and bypasses React’s internals completely. If you simply call a function you never trigger the tracking that allows React to register and call hooks properly. What’s worse, in certain cases that may not trigger an error, you may actually end up registering the hooks on the wrong component! Bottom line: a function only becomes a component to the React core when you actually render it --as is evidenced by the workaround above, so render it!

https://kentcdodds.com/blog/dont-call-a-react-function-component

Please fix this. It wouldn’t be a minor issue for any React framework

(Elastic is great. Keep up the good work)

4reactions
FokkeZBcommented, Feb 26, 2021

For others who run into this, you can workaround via:

const MyResultView = () => {
  const someHook = useHook();

  return <div>My Result</div>;
};

const MyPatchedResultView = (props) => (
  <MyResultView {...props} />
);

<Result view={MyPatchedResultView} />
Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid hook call. Hooks can only be called inside of the ...
Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: You...
Read more >
Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
Read more >
Invalid Hook Call Warning
You can only call Hooks while React is rendering a function component : ✓ Call them at the top level in the body...
Read more >
What will happen to the Container/Presenter Pattern? - YouTube
React Hooks : What will happen to the Container /Presenter Pattern?
Read more >
Best Practices With React Hooks
Don't call Hooks inside loops, conditions, or nested functions. Always use Hooks at the top level of your React function.
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