I'm trying out create-react-library to bundle a reusable library. The idea I'm trying out is to create composable component libraries that we can use in our web app and our electron app.
In our package.json file we have the following requirements
"peerDependencies": {
"react": "^15.0.0 || ^16.0.0",
"react-dom": "^15.0.0 || ^16.0.0"
},
"devDependencies": {
"@material-ui/core": "^4.0.0-alpha.4",
....
"react": "^16.4.1",
"react-dom": "^16.4.1",
}
When I import a Material UI component it results in an error
Hooks can only be called inside the body of a function component. https://reactjs.org/warnings/invalid-hook-call-warning.html
This is the full component (it's just the example from create-react-library that I was slowly expanding on)
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Button } from "@material-ui/core";
import styles from './styles.css'
class ExampleComponent extends Component {
static propTypes = {
text: PropTypes.string
}
render() {
const { text } = this.props
return (
<div className="">
<Button></Button>
Example Component: {text}
</div>
)
}
}
export default ExampleComponent;
********************************** Edit **********************************
Changing the devDependencies for material ui and react and adding to the rollup.config.js file (to deal with an issue that this introduced) fixed the error
"devDependencies": {
"@material-ui/core": "^3.9.0",
...
"react": "^16.7.0",
"react-dom": "^16.7.0",
}
rollup.config.js
commonjs({
include: 'node_modules/**',
namedExports: {
'node_modules/@material-ui/core/styles/index.js': [
'createGenerateClassName',
'createMuiTheme',
'createStyles',
'jssPreset',
'MuiThemeProvider',
'withStyles',
'withTheme'
],
'node_modules/@material-ui/core/Modal/index.js': [ 'ModalManager' ]
}
})
This problem can also come up when you use npm link or an equivalent. In that case, your bundler might “see” two Reacts — one in application folder and one in your library folder. Assuming myapp and mylib are sibling folders, one possible fix is to run npm link ../myapp/node_modules/react from mylib .
This could happen for one of the following reasons: You might have mismatching versions of React and the renderer (such as React DOM) You might be breaking the Rules of Hooks. You might have more than one copy of React in the same app See for tips about how to debug and fix this problem.
Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any early returns. By following this rule, you ensure that Hooks are called in the same order each time a component renders.
In my case, I had a submodule installed with a node_modules
folder, I was actually compiling react twice into a bundle.
Fixed by removing submodule's node_modules folder, could also potentially fix in webpack config:
main
node_modules
submodules
-> node_modules
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With