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.

Request for help bundling chakra

See original GitHub issue

I’ve built some components that wrap chakra components. I’m trying to strip them out into their own library that I can include in other projects. I’m receiving an error on loading my apps that I include this library into when I try to use toasts.

I’ve tried several different packages to build it, including rollup, microbundle, and tsdx. The error message I am getting is “Invalid hook call”.

Screen Shot 2020-03-14 at 17 20 09

I was wondering if anyone has successfully accomplished something similar and would be generous to write something up on how it was done.

I’ve extracted it to the most basic recreation that I can come up with and uploaded it here.

https://github.com/dusty/chakra-tester

The entire library consists of this:

import { Box, BoxProps, Button, useToast } from '@chakra-ui/core'
import React, { FC, forwardRef } from 'react'

export const MyBox: FC<BoxProps> = forwardRef(({ children, ...rest }, ref) => {
  const toast = useToast()
  const sendToast = () => {
    toast({
      status: 'success',
      description: 'hello',
      duration: 3000,
      isClosable: true,
      position: 'top-right',
    })
  }
  return (
    <Box ref={ref} {...rest}>
      <Button onClick={sendToast}>{children}</Button>
    </Box>
  )
})

The example app trying to use this library consists of the following:

import { CSSReset, theme, ThemeProvider } from '@chakra-ui/core';
import { MyBox } from 'chakra-tester';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <CSSReset />
      <MyBox>Hello</MyBox>
    </ThemeProvider>
  );
};

ReactDOM.render(<App />, document.getElementById('root'));

Does anyone have any ideas on this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ifedyukincommented, Apr 9, 2020

I’ve got the same issue with my Webpack based library that uses Chakra-UI. I solved this problem with some minor fixes in my webpack.config.js:

externals: {
  // mark "react" and react-dom as external packages that will import using commons
  react: 'commonjs react',
  'react-dom': 'commonjs react-dom'
},
output: {
  path: path.resolve('./'),
  libraryTarget: 'commonjs2', // set library target
  filename: '[name].js',
  library: '',
}

Also, webpack-node-externals can help too.

1reaction
dustycommented, Apr 7, 2020

Here is my config that is working with tsdx - although I’d love to give a try using just babel and tsc.

tsdx@0.13.1

tsconfig.json

{
  "include": ["src", "types"],
  "compilerOptions": {
    "module": "esnext",
    "lib": ["dom", "esnext"],
    "importHelpers": true,
    "declaration": true,
    "sourceMap": true,
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "@": ["./"],
      "*": ["src/*", "node_modules/*"]
    },
    "jsx": "react",
    "esModuleInterop": true
  }
}

The example directory to run the code uses this package.json setup.

{
  "name": "example",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build index.html"
  },
  "dependencies": {
    "react-app-polyfill": "^1.0.0"
  },
  "alias": {
    "react": "../node_modules/react",
    "react-dom": "../node_modules/react-dom/profiling",
    "scheduler/tracing": "../node_modules/scheduler/tracing-profiling"
  },
  "devDependencies": {
    "@babel/core": "7.9.0",
    "@types/react": "16.9.32",
    "@types/react-dom": "16.9.6",
    "parcel": "1.12.4",
    "typescript": "3.8.3"
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Ultimate Chakra Healing Bundle - Zanna Keithley
Download the Ultimate Chakra Healing Bundle, which includes a Chakra Healing eBook, Chakra Healing Workbook, and 8-Week Chakra Healing Guide.
Read more >
Chakra Balancing Bundle - Heritage Apothecary
Harmonize your chakras with the purifying power of sage and balancing energy of rose. Each vibrant petal aligns with one of the seven...
Read more >
Your Complete Guide To The Body Chakras – Forbes Health
The term “chakra” is a common phrase in Ayurveda, yoga and other traditional holistic medicine practices. Learn all about the ancient energy ...
Read more >
Chakra Bundle - Etsy
Check out our chakra bundle selection for the very best in unique or custom, handmade pieces from our reiki & chakras shops.
Read more >
Chakra Healing: 2 in 1 Bundle - Audible.com
The Definitive Guide to Awaken and Balance Your 7 Chakras. 2 in 1 Bundle with Chakra Healing for Beginners and Guided Meditations from...
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