I’m working with a monorepo in which I want to create one folder containing all those little functions as well as components all the separate projects in that monorepo use.
I figured, the best way to do this would be creating a node module in the monorepo and having it export those common functions and components.
Now, when I try to use my exported component from that “common” sub-project, I get messages like this:
dispatcher is null
useState@http://localhost:3000/static/js/bundle.js:162692:7
$@http://localhost:3000/static/js/bundle.js:158032:34
renderWithHooks@http://localhost:3000/static/js/bundle.js:114086:31
...
I’ve created the following setup:
common/
lib/
components/
Tooltip/
index.tsx
helpers/
API.ts
DATETIME.ts
...
index.ts
...configs
index.ts
import Tooltip from './components/Tooltip';
export * from './helpers/API';
export * from './helpers/DATETIME';
...
export const COMP = {
Tooltip: Tooltip
...
}
Note: the wrapping into that const object is to facilitate organization as this library will grow.
Tooltip/index.jsx
import {FC, ReactElement, cloneElement, useState} from 'react';
export interface ITooltipProps {
children: ReactElement<any, string>,
anchor: ReactElement<any, string>
}
const Tooltip: FC<ITooltipProps> = (props: ITooltipProps) => {
const [isOpen, setIsOpen] = useState(false);
return (
<div className='tooltip-wrapper' style={{
position: 'relative',
display: 'inline-block'
}}
onClick={() => setIsOpen(true)}
onMouseOver={() => setIsOpen(true)}
onMouseLeave={() => setIsOpen(false)}
>
{cloneElement(props.anchor, {className: 'tooltip-anchor'})}
<div style={{
visibility: isOpen ? 'visible' : 'hidden',
zIndex: 10,
top: '100%',
left: '50%',
opacity: isOpen ? 1 : 0,
transition: 'all ease-in-out 0.3s'
}}>
{props.children}
</div>
</div>
)
}
export default Tooltip;
Note: I know this “tooltip” is incredibly stupid right now but it just serves as a placeholder to figure out this components-in-separate-package issue.
package.json
...
"main": "dist/index.js",
"directories": {
"lib": "lib"
},
"scripts": {
"build": "tsup",
"ladle": "ladle serve --stories=lib/components/**/*.stories.tsx"
},
"devDependencies": {
"@ladle/react": "^4.0.2",
"@types/react": "^18.2.63",
"tsup": "^8.0.2",
"typescript": "^5.3.3"
},
"dependencies": {
"buffer": "^6.0.3"
},
"peerDependencies": {
"buffer": "^6.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
tsconfig.json
{
"compilerOptions": {
"target": "es2017",
"module": "esnext",
"lib": [ "esnext", "dom" ],
"jsx": "react-jsx",
"sourceMap": true,
"moduleResolution": "node",
"preserveConstEnums": true,
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"declaration": true
}
}
tsup.config.json
{
"splitting": true,
"sourcemap": true,
"clean": true,
"minify": true,
"dts": true,
"entry": [
"lib/index.ts"
]
}
In the other projects in this same repo, this “common”-package is added to dependencies as "common": "file:../common-js".
