reactjs - EmotionJS 组件库中的组件未收到 Theme 属性

标签 reactjs typescript next.js emotion

我正在创建一个基于 Emotion 和 Typescript 的组件库。
但是,当我使用 EmotionJS 和 NextJS 将组件导入到不同的项目中时,它不接受 Theme 属性。

这是组件存储库:
https://github.com/wilsonmsalberto/emotionjs-component-library

这是我重现错误的存储库:
https://github.com/wilsonmsalberto/emotionjs-component-library-nextjs

我的目标是使用组件存储库来拥有多个主题和多个组件。

然后,在任何 EmotionJS 项目中,我都会导入主题和任何组件,并像平常一样在 Emotion 中使用它。

我直接构建到存储库中并直接从 git 调用生成的组件。

目前,我收到以下错误

enter image description here

最佳答案

据我所知,您的问题与在库和nextJS应用程序之间正确解析node_modules有关(可能从不同的node_modules实例化2个不同的提供程序)。

这在处理本地链接包时更常见,但我认为你在这里也有类似的问题。请参阅https://github.com/emotion-js/emotion/issues/1107了解更多详情。

要解决此问题,请尝试配置 nextJS 的 webpack 以在本地解决情感依赖问题,方法是将其放入 <app_root>/next.config.js .

const path = require('path');

module.exports = {
  webpack: (config) => {

    // resolve "@emotion/core" and related dependencies locally
    config.resolve.alias['@emotion/core'] = path.resolve('./node_modules/@emotion/core');
    config.resolve.alias['@emotion/styled'] = path.resolve('./node_modules/@emotion/styled');
    config.resolve.alias['emotion-theming'] = path.resolve('./node_modules/emotion-theming');

    return config
  },
}

希望它有效!

关于reactjs - EmotionJS 组件库中的组件未收到 Theme 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64448935/

相关文章:

ios - 我的 React 应用程序无法在旧版 Safari 浏览器上加载图像和某些组件。显示 SSL 错误

node.js - 如何部署分离的前端和后端?

javascript - NextJS - 使用浅路由器推送时无法识别页面组件上发生状态更改的位置

caching - 如何使用 Vercel/Next.js 改进字体加载/缓存?

reactjs - 使用 MUI withStyle 和 TypeScript 以及 next.js 覆盖颜色的正确方法是什么?

javascript - 将 NProgress 与 "React.lazy"一起使用

javascript - 如何根据 TypeScript 中的条件在数组对象中创建新属性

javascript - 为什么要为 Javascript 库在绝对类型上发布 TypeScript 声明文件?

typescript 和 babel-plugin-module-resolver : VS Code doesn't resolve imports

javascript - 如何使用 componentWillUnmount 删除 React.js 中的 setInterval