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.

When passing a component to a library package: “Hook can only be invoked from render methods”

See original GitHub issue

I’ve read everything I could find on the issue, tried various workarounds, but nothing worked. That may be because my setup is slightly unusual (but the repro project is simple).

As an aside, I’m getting the same error if I try this with React.

Repositories for reproducing the problem:

preact-repro-lib/ts/index.tsx:

import { VNode } from 'preact';
import render from 'preact-render-to-string';

export type SimpleView = () => VNode;

export function logRenderedView(View: SimpleView) {
  console.log('----------');
  console.log(render(<View/>));
  console.log('----------');
}

preact-repro-app/ts/main.tsx:

import { logRenderedView } from 'preact-repro-lib';
import { useState } from 'preact/hooks';

import 'preact/debug';

function MyComponent() {
  const [clickCount, setClickCount] = useState(0);
  return <div>
    <a href="" onClick={handleClick}>{clickCount}</a>
  </div>;

  function handleClick(event: MouseEvent) {
    event.preventDefault();
    setClickCount(clickCount+1);
  }
}

logRenderedView(MyComponent);

Output:

Error: Hook can only be invoked from render methods.
    at Object.n.__h (file:///Users/rauschma/tmp/preact-repro-app/node_modules/preact/debug/dist/debug.mjs:1:6090)
    at m (file:///Users/rauschma/tmp/preact-repro-app/node_modules/preact/hooks/dist/hooks.mjs:1:125)
    at p (file:///Users/rauschma/tmp/preact-repro-app/node_modules/preact/hooks/dist/hooks.mjs:1:280)
    at l (file:///Users/rauschma/tmp/preact-repro-app/node_modules/preact/hooks/dist/hooks.mjs:1:249)
    at Object.MyComponent (file:///Users/rauschma/tmp/preact-repro-app/dist/main.js:6:41)
    at x (file:///Users/rauschma/tmp/preact-repro-lib/node_modules/preact-render-to-string/dist/index.mjs:1:2257)
    at m (file:///Users/rauschma/tmp/preact-repro-lib/node_modules/preact-render-to-string/dist/index.mjs:1:1077)
    at logRenderedView (file:///Users/rauschma/tmp/preact-repro-lib/dist/index.js:5:17)
    at file:///Users/rauschma/tmp/preact-repro-app/dist/main.js:13:1
    at ModuleJob.run (node:internal/modules/esm/module_job:175:25)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
marvinhagemeistercommented, Jul 17, 2021

Good point, pnpm workspaces work too 👍

1reaction
marvinhagemeistercommented, Jul 17, 2021

You sound like there is no real solution(?)

My comment was aimed at just relying on npm. The file syntax for dependencies only works for simple cases as it doesn’t really understand the relationship between those two folders. It breaks for anything else.

Tools like yarn workspaces or lerna solve exactly this problem, which happens to be the same one as for monorepos where folders containe dependencies that depend on each other. Afaik npm is in the process of adding decent workspace support natively, but that may take a while.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Debugging Preact Apps
Hook can only be invoked from render methods. This error occurs when you try to use a hook outside of a component. They...
Read more >
typeerror: cannot read properties of undefined (reading '__h') - You ...
I am building a library in preact for use with preact apps and widgets. ... other is 'preact Uncaught Error: Hook can only...
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