简体   繁体   English

如何在 React 中导入 Bootstrap 组件?

[英]How to import Bootstrap components in React?

I'm trying to integrate checkboxes from bootstrap or reactstrap in React.我正在尝试在 React 中集成来自bootstrapreactstrap的复选框。 I'm getting a common error, looked into relevant posts , still cannot figure it out.我遇到了一个常见错误,查看了相关帖子,仍然无法弄清楚。 How do I fix this error:如何解决此错误:

Error Element 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.您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

Source of Error错误来源

The problem should be here in these two lines, which I'm not sure what it is.问题应该在这两行中,我不确定它是什么。

import { InputGroup, InputGroupAddon, InputGroupText } from 'reactstrap';
import FormControl from 'react-bootstrap/FormControl';

If we'd remove these lines and the HTML copied below, it does not give any Error.如果我们删除这些行和下面复制的 HTML,它不会给出任何错误。

HTML HTML

          <div>
            <InputGroup className="mb-3">
              <InputGroup.Prepend>
                <InputGroup.Checkbox aria-label="Checkbox for following text input" />
              </InputGroup.Prepend>
              <FormControl aria-label="Text input with checkbox" />
            </InputGroup>
            <InputGroup>
              <InputGroup.Prepend>
                <InputGroup.Radio aria-label="Radio button for following text input" />
              </InputGroup.Prepend>
              <FormControl aria-label="Text input with radio button" />
            </InputGroup>
          </div>

Demo演示

[Thanks. [谢谢。 I'm new to React.]我是 React 的新手。]

prepend is one value of the addonType prop of InputGroupAddOn or InputGroupButton. prepend是 InputGroupAddOn 或 InputGroupButton 的addonType一个值。 it's not a property of the InputGroup import.它不是InputGroup导入的属性。 InputGroup.Prepend is undefined, which is why React is complaining. InputGroup.Prepend未定义,这就是 React 抱怨的原因。

according to the reactstrap docs , you want this:根据reactstrap docs ,你想要这个:

InputGroupAddOn.propTypes = {
  tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  addonType: PropTypes.oneOf(['prepend', 'append']).isRequired,
  className: PropTypes.string
};

InputGroupButton.propTypes = {
  tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  addonType: PropTypes.oneOf(['prepend', 'append']).isRequired,
  children: PropTypes.node,
  groupClassName: PropTypes.string, // only used in shorthand
  groupAttributes: PropTypes.object, // only used in shorthand
  className: PropTypes.string
};

In your component you camelcase inputGroupAddOn when declaring the prop types.在您的组件中,您在声明道具类型时将inputGroupAddOn When you imported it you didn't camel case the add on part, you're importing InputGroupAddon .当您导入它时,您没有将添加部分放在驼峰式大小写中,而是导入InputGroupAddon That might be another issue you're having.那可能是您遇到的另一个问题。

The Latest version of reactstrap doesn't support the use of InputGroupAddon .最新版本的reactstrap不支持使用InputGroupAddon Use it directly from bootstrap.直接从引导程序中使用它。

<div className="input-group-append">
  <span className="input-group-text" id="basic-addon2">Search</span>
</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM