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.

Hooks error on React library compiled with Rollup.js

See original GitHub issue

Disclaimer: I’m not sure if this is an specific bug from React’s Hooks or it is something I’m missing in my Rollup.js configuration but I’ve been trying a lot of different things and always end up in the same error.

Hooks can only be called inside the body of a function component.

I’m building a React Component library using the alpha version of React and Hooks, and using Rollup.js v1.1.2 as a bundler with the next configuration:

import { readdirSync } from 'fs';
import path from 'path';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import external from 'rollup-plugin-peer-deps-external';
import replace from 'rollup-plugin-replace';
import resolve from 'rollup-plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';

const CODES = [
  'THIS_IS_UNDEFINED',
  'MISSING_GLOBAL_NAME',
  'CIRCULAR_DEPENDENCY',
];

const getChunks = URI =>
  readdirSync(path.resolve(URI))
    .filter(x => x.includes('.js'))
    .reduce((a, c) => ({ ...a, [c.replace('.js', '')]: `src/${c}` }), {});

const discardWarning = warning => {
  if (CODES.includes(warning.code)) {
    return;
  }

  console.error(warning);
};

const env = process.env.NODE_ENV;

const plugins = [
  external(),
  babel({
    exclude: 'node_modules/**',
  }),
  resolve(),
  replace({ 'process.env.NODE_ENV': JSON.stringify(env) }),
  commonjs(),
  env === 'production' && terser(),
];

export default [
  {
    onwarn: discardWarning,
    input: 'src/index.js',
    output: {
      esModule: false,
      file: 'umd/library-name.js',
      format: 'umd',
      name: 'libraryName',
    },
    plugins,
  },
  {
    onwarn: discardWarning,
    input: getChunks('src'),
    output: [
      { dir: 'esm', format: 'esm', sourcemap: true },
      { dir: 'cjs', format: 'cjs', sourcemap: true },
    ],
    plugins,
  },
];

For the examples page I created an internal project using create-react-app and I’m importing the library in the package.json using link:..

The page breaks as soon as it gets to the first Hooks call var svgRef = useRef(); with the error mentioned above, but this only happens when I’m using the bundled code from the ESM file not when I’m using the library code directly. That’s why I’m not sure if it is a Rollup misconfiguration or a React Hooks bug.

I appreciate any help or guidance on this.

React v16.8.0-alpha.1 React DOM v16.8.0-alpha.1 OS: macOS Browser: Chrome Node.js v11.8.0

I did test it with a previous alpha version of React and it happens the same.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:28 (3 by maintainers)

github_iconTop GitHub Comments

24reactions
gaearoncommented, Jan 29, 2019

You probably have two Reacts.

21reactions
gaearoncommented, Jan 30, 2019

I can’t guess what’s wrong in your particular setup but I’ve described the issue: you’ll see it when you have two Reacts in the resulting bundle. Check the bundle contents. I can’t help you with setting up Rollup, what its errors mean, or how Node resolutions works — just the React part which is definitely related to that.

Npm link is known for causing this because webpack would “see” React both in your lib folder and in your app folder, and would include both of them in the bundle. A common workaround is to npm link back from your lib’s node_modules/react into your app’s React copy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React + Antd + Rollup Component Library "Error: Invalid hook ...
If this issue happens while you're linking the local version of your library in your main project to speed up the development.
Read more >
linking libraries: react hooks - code-comments
Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one...
Read more >
Solving React Hooks' invalid hook call warning
Fix the invalid hook call warning using Webpack ... It's probably less likely that you'll have a Parcel JS specific problem here, but...
Read more >
Invalid Hook Call Warning - React
You are probably here because you got the following error message: Hooks can only be called inside the body of a function component....
Read more >
Rollup Config for React Component Library With TypeScript + ...
config.js. import resolve from '@rollup/plugin-node-resolve' ...
Read more >

github_iconTop Related Medium Post

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