I've been trying to create my own Component Library for the past few weeks, everything is going smooth except from the fact that I can't get hooks to work. Whenever I import from the library a component that uses hooks I get "Hooks can only be called inside the body of a function component."
https://reactjs.org/warnings/invalid-hook-call-warning.html
I would expect it's something about React being duplicated which I don't see when I run npm ls react
.
This is my webpack.config.js
const path = require('path');
var nodeExternals = require('webpack-node-externals');
module.exports = {
entry: './src/index.js',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
}
},
{
test: /\.scss$/,
sideEffects: true,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
{ loader: 'sass-loader' },
],
},
{
test: /\.(png|gif|jpg|svg)$/,
use: {
loader: 'url-loader',
options: {
limit: 50000,
},
},
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/'
}
}
]
}
],
},
resolve: {
extensions: ['.scss', '.ttf', '.js', '.json', '.png', '.gif', '.jpg', '.svg'],
},
output: {
path: path.resolve(__dirname, 'dist/'),
publicPath: '',
filename: 'index.js',
libraryTarget: 'umd',
},
target: 'node',
externals: [ nodeExternals() ]
};
This is my package-json
{
"name": "ui-components",
"version": "1.1.1",
"description": "componentes UI",
"main": "dist/index.js",
"scripts": {
"test": "jest",
"build": "webpack --mode production",
"storybook": "start-storybook -p 9001",
"docz:dev": "docz dev",
"docz:build": "docz build"
},
"repository": {
"type": "git",
"url": "..."
},
"keywords": [
"react",
"shared",
"components",
"botbit"
],
"author": "Uriel Chami",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"@storybook/addon-actions": "^5.0.0",
"@storybook/addon-storyshots": "^5.1.10",
"@storybook/react": "^5.0.0",
"@tulipjs/eslint-config": "^1.1.1",
"babel-loader": "^8.0.1",
"babel-plugin-macros": "^2.6.1",
"css-loader": "^1.0.0",
"docz": "^1.2.0",
"docz-theme-default": "^1.2.0",
"file-loader": "^4.1.0",
"jest": "^24.8.0",
"node-sass": "^4.9.3",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-router-dom": "^5.0.1",
"react-test-renderer": "^16.8.6",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"url-loader": "^1.1.2",
"webpack": "^4.28.3",
"webpack-cli": "^3.2.0",
"webpack-node-externals": "^1.7.2"
},
"peerDependencies": {
"react": "16.9.0",
"react-dom": "16.9.0"
},
"dependencies": {
"mdbreact": "..."
}
}
Im using npm link
to test the functioning of the library but also I can deploy it to GitHub and npm update
it.
I've added window.React1 = require('react');
in node_modules/react-dom/index.js (in the App that uses my library).
And
require('react-dom');
window.React2 = require('react');
console.log(window.React1);
console.log(window.React1 === window.React2);
in my App.js (in the app that uses my library).
And it throws true
. I'm slowly getting clueless. Any ideas?
Edit:
The code where I'm trying to use Hooks is:
import React, {useEffect} from 'react';
export const Test = () => {
useEffect(()=>{
console.log("component did mount");
} , [])
return(<div>Hola!</div>);
}
Edit 2
When I build yarn webpack --mode development
it throws 'require is not defined'.
Here it goes a GitHub repo with my complete code if anyone wants to toy around: https://github.com/uchami/hooks-not-working
Edit 3
I add the component library (ui-components) as so yarn add git+ssh:repository
.
The application where I import my library is simply a plain create-react-app.
And I just implement the Test
component (which is the one that uses Hooks) using import {Test} from 'ui-components'
and I use it just like that <Test></Test>
.
The compilation of the ui-components
project is just yarn build
on the console. On the background what is doing is calling yarn webpack --mode production
. If you call yarn webpack --mode development
you will see extra verbose error output.