【问题标题】:Accessing dependencies for react-toggle-component访问 react-toggle-component 的依赖项
【发布时间】:2019-03-13 20:28:37
【问题描述】:

我正在尝试使用这个 React 库 (https://www.npmjs.com/package/react-toggle-component),但很难让它工作。

我已经通过 npm 安装了所需的依赖项(react、react-circle、react-dom 和 styled-components)。 安装这些依赖项并尝试安装 react-toggle-component 后,我​​收到错误“找不到模块“react”的声明文件。尝试npm install @types/react(样式组件的相同错误)。所以我按照这个命令成功安装了react-toggle-component。

现在,当我尝试在我的 react 组件中使用该组件时,我正在使用它,就像在建议的用法中一样:

import React from "react"

import Toggle from "react-toggle-component"

function Application() {

  return <Toggle name="toggle-1" />;

}

但是当我运行我的应用程序时,我收到以下错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

我不完全明白为什么我必须安装 react 和 styled-components 的类型定义,我想知道这是否会导致我的应用程序找不到 Toggle 组件。

如果你能指出我正确的方向,或者给我任何理解这一点的帮助,我将非常感激!

【问题讨论】:

    标签: node.js reactjs


    【解决方案1】:

    显然文档是错误的。该组件似乎已作为命名导出导出,因此我必须使用以下命令导入:

    import {Toggle} from "react-toggle-component"
    

    根据这里的解释:https://github.com/facebook/react/issues/12214

    【讨论】:

      猜你喜欢
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2022-06-12
      相关资源
      最近更新 更多