简体   繁体   English

如何使用 `require` 而不是 `include` 包含我的自定义组件

[英]How do I include my custom Component using `require` instead of `include`

I have built a simple React Component using nwb as described in this guide . 如本指南中所述,我使用nwb构建了一个简单的 React 组件。

It's a very simple Component that is just a button:这是一个非常简单的组件,只是一个按钮:

import t from 'prop-types'
import React, {Component} from 'react'

class LoadingButton extends Component {
  static propTypes = {
    disabled: t.bool,
    loading: t.bool,
    type: t.string,
  }
  static defaultProps = {
    disabled: false,
    loading: false,
    type: 'button',
  }
  render() {
    let {children, disabled, loading, type, ...props} = this.props
    if (loading) {
      disabled = true
    }
    return <button disabled={disabled} type={type} {...props}>
      {children}
    </button>
  }
}

export default LoadingButton

In another project, after using npm link , I am able to import this Component doing something like this:在另一个项目中,使用npm link后,我可以导入此组件,执行如下操作:

import LoadingButton from 'react-loading-button'

And it works!它有效!

在此处输入图像描述

But my question is, I also need to include this Component using require (inside an older codebase).但我的问题是,我还需要使用require包含此组件(在较旧的代码库中)。 I would like to do something like this:我想做这样的事情:

var LoadingButton = require("react-loading-button");

Unfortunately, this approach doesn't work for me.不幸的是,这种方法对我不起作用。 It gives me this error:它给了我这个错误:

Error: Objects are not valid as a React child (found: [object Module]). If you meant to render a collection of children, use an array instead.

在此处输入图像描述

I have built the component using nwb, which states:我已经使用 nwb 构建了该组件,其中指出:

By default, nwb will create a CommonJS build for your project in lib/, which is the primary way it will be used when installed via npm, with default package.json main config pointing to lib/index.js.默认情况下,nwb 将在 lib/ 中为您的项目创建一个 CommonJS 构建,这是通过 npm 安装时使用的主要方式,默认 package.Z466DEEC76ECDF5FCA6D38571F6324D54 配置指向。

So I am a bit confused about why the require doesn't work.所以我对为什么require不起作用有点困惑。

Has anyone had any experience with this approach?有没有人有过这种方法的经验?

I tried with function/class style components, var LoadingButton = require("react-loading-button").default;我尝试使用函数/类样式组件var LoadingButton = require("react-loading-button").default; seems working fine, codebase is not exactly the same, but worth trying.似乎工作正常,代码库并不完全相同,但值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的 angular 2 项目中正确包含“require”? - How do I correctly include "require" in my angular 2 project? 如何手动在组件中包含“ @ material / drawer”? - How do I manually include “@material/drawer” into my component? 如何使用nodejs包含自定义记录器? - How should I include my custom logger using nodejs? 如何安全地运行/包含/需要远程 javascript? - How do I run/include/require remote javascript safely? (Webpack) 使用 url-loader 或 file-loader,我真的必须在我的 .js 中为我想要包含的每个静态图像包含一个 require() 吗? - (Webpack) Using the url-loader or file-loader, do I really have to include a require() in my .js for every static image I want to include? 在 Angular 14 中,如何在独立组件中包含子组件? - In Angular 14, how do I include a child component in a standalone component? 如何在PhpStorm中为我的JavaScript代码添加“ require”? - How to include “require” in PhpStorm for my JavaScript code? 如何在我的Jenkins构建中排除/包含某些component.spec.ts文件 - How do I exclude/include certain component.spec.ts files from running in my Jenkins build 在使用cq:include包含外部JS之前,如何在JSP中包含全局JS变量? - How do I include global JS variables in JSP, before I include external JS using cq:include? 如何在查询中包含自定义 SELECT - How can I include custom SELECTs in my queries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM