javascript - 如何在React中导入Bootstrap组件?

标签 javascript html css reactjs react-bootstrap

我正在尝试在 React 中集成来自 bootstrapreactstrap 的复选框。我遇到了一个常见错误,请查看 relevant posts ,还是想不通。如何修复此错误:

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.

错误来源

问题应该出在这两行,我不确定它是什么。

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

如果我们删除这些行和下面复制的 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

[谢谢!我是 React 新手。]

最佳答案

prepend 是InputGroupAddOn 或InputGroupButton 的addonType 属性的一个值。它不是 InputGroup 导入的属性。 InputGroup.Prepend 未定义,这就是 React 提示的原因。

根据the 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
};

关于javascript - 如何在React中导入Bootstrap组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60794714/

相关文章:

javascript - 如何管理未知数量的文本区域的状态?

javascript - 使用 X 类 JavaScript 更改所有链接的颜色

php - 如何将未选中的复选框提交给 Controller

javascript - 将多个值传递/发送到具有 var 名称和元素的 jQuery 函数 [init(div :'#div' ,txt :'hello' )/init(div ='#div' ,txt ='hello' )]

javascript - 在 HTML 中使用 PHP 文件

javascript - 使用 Phonegap 3.3 在 Safari 浏览器中打开的链接

javascript - 防止外部组件在顶部添加空白

javascript - 我怎样才能隐藏整个 div block 的街道地址并只显示公司 block ?

html - id 不适用于移动浏览器的 HTML 元素

javascript - 使 Chart.js Canvas 在可调整大小的 div 内响应