【问题标题】:Can't apply bootstrap to react (type is invalid)无法应用引导程序做出反应(类型无效)
【发布时间】:2019-11-16 16:41:48
【问题描述】:

我无法将 Bootstrap 应用到我的项目中。

import {Button, Form, Label, Input, Modal, ModalBody} from 'react- 
bootstrap';

在我的 index.html 中,我有一个指向 CDN 的链接。 我很困惑,因为 import 在非常相似的项目中工作,但使用了另一个导入的组件。

My render method 
render() {
      return (
        <div>
        ...
        <Modal name='modal' isOpen={this.state.showModal}> 
          <ModalBody>
             Please add a popup for a location.
              <Form name="form">
            <Label for="Popup">Popup</Label>
            <Input type="text" name="popup" id="popup" placeholder="Popup"/> 
            <Button bsStyle="primary" type="submit"
            onClick={(e)=>this.submit(e)}>Add</Button>
             </Form>
         </ModalBody></Modal>
        </div>
      )
    }
  }

我对 MapComponent 组件的导入

    import React, {Component} from 'react';
    import {Button,  Form, Label,  Input, Modal, ModalBody} from 'react- 
     bootstrap';
import { Map, TileLayer, Marker, Popup } from 'react-leaflet'

我对 App 组件的导入

import 'babel-polyfill';
import ReactDOM from 'react-dom';
import React from 'react';
import { Router, Route, hashHistory } from 'react-router';
import MapComponent from './MapComponent.jsx';
import styles from '../../css/styles.css';

警告:React.createElement:类型无效 - 应为字符串(用于内置组件)或类/函数(用于复合组件)但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

检查MapComponent的渲染方法。 在 MapComponent 中(由 RouterContext 创建) 在RouterContext(由Router创建) 在Router中(由App创建) 应用内

【问题讨论】:

  • 你能把你要导入的东西贴在这个文件里吗?
  • 您的更新部分拯救了这一天!问题是我对 reactstrap 和 bootstrap 感到困惑。将所有样式更改为一种样式使其起作用!感谢您为我提供指导。

标签: react-bootstrap


【解决方案1】:

使用CDN 链接,您无法导入类似的模块,

import {Button, Form, Label, Input, Modal, ModalBody} from 'react- 
bootstrap';

你需要安装react-bootstrap

npm install react-bootstrap --save

yarn add react-bootstrap

更新

您正在从react-bootstrap 导入{Button, Form, Label, Input, Modal, ModalBody},但您正在为您的模态使用reactstrap 代码,

import {Button, Form, Label, Input, Modal, ModalBody} from 'react-bootstrap';

这是reactstrap modal 的代码

<Modal name='modal' isOpen={this.state.showModal}> 
          <ModalBody>
             Please add a popup for a location.
              <Form name="form">
            <Label for="Popup">Popup</Label>
            <Input type="text" name="popup" id="popup" placeholder="Popup"/> 
            <Button bsStyle="primary" type="submit"
            onClick={(e)=>this.submit(e)}>Add</Button>
             </Form>
         </ModalBody></Modal>

您可以更改导入以使您的代码正常工作

import {Button, Form, Label, Input, Modal, ModalBody} from 'reactstrap';

如果你想使用react-bootstrap modal 那么这是modal的代码

<Modal.Dialog>
  <Modal.Header closeButton>
    <Modal.Title>Modal title</Modal.Title>
  </Modal.Header>

  <Modal.Body>
    <p>Modal body text goes here.</p>
  </Modal.Body>

  <Modal.Footer>
    <Button variant="secondary">Close</Button>
    <Button variant="primary">Save changes</Button>
  </Modal.Footer>
</Modal.Dialog>

【讨论】:

  • 同时发布您的package.json 文件。
  • "dependencies": { "body-parser": "^1.15.2", "bootstrap": "^4.3.1", "express": "^4.17.1", "leaflet ": "^1.5.1", "mongodb": "^2.2.6", "react-leaflet": "^1.9.1", "reactstrap": "^8.0.0" }, "devDependencies": { “babel-loader”:“^6.2.5”,“babel-polyfill”:“^6.13.0”,“babel-preset-es2015”:“^6.9.0”,“babel-preset-react”:“ ^6.11.1”、“css-loader”:“^3.0.0”、“nodemon”:“^1.10.0”、“react”:“^16.8.6”、“react-bootstrap”:“^0.32 .1", "react-router": "^3.2.0", "style-loader": "^0.23.1", "webpack": "^1.13.2", },
  • 使用您遇到此错误的组件代码更新您的问题。
  • 在“静态”文件夹中,我创建了一个名为 bootstrap 的文件,其内容为“../node_modules/bootstrap/dist”,并带有指向 CDN 的链接,它运行良好。我的意思是,在我之前的项目中。
  • 我们无法仅仅阅读解释,显示您的代码。错误也清楚地表明您错过了导出某些内容。
猜你喜欢
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 2021-12-30
  • 2019-11-11
  • 1970-01-01
  • 2019-09-15
相关资源
最近更新 更多