html - 如何使用 bootstrap 和 reactjs

标签 html reactjs bootstrap-4

我是 React.js 的新手,自从我开始阅读文档和教程后,我不确定 Bootstrap 如何与 React.js 一起工作。

我看到了几个 npm 包,例如 reactstrapreact-bootstrap ,但是,我不明白。 HTML 代码来自一个返回一段 HTML 的方法(像这样):

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

如果我是对的......

所以,我的问题是:

  • 我应该添加 <link/> 吗?或 <script/>将 Bootstrap 放入 index.html,放入正文或标题?

  • 我是否必须从我的 App.js 中包含 Bootstrap?文件并从方法中使用它?

PS:我已经开发多年了,但我从未做过任何 Javascript/HTML/CSS web,所以如果您有任何建议,请提前致谢!

我需要使用Bootstrap网格的逻辑来实现一个HCI

感谢您的帮助

最佳答案

要回答您的问题,您对问题的两个建议都是正确的。您可以采用任何一种方式。

  1. 在您的 index.html 中包含 bootstrap CDN
  2. 您可以添加 Bootstrap 包,例如 React-Bootstrap使用 npm/yarn 并将所需的组件导入到你的 App.js

如果您采用第一种方式,您需要做的就是添加您的 Bootstrap CDNindex.html 中,当您使用 Javascript 访问您的类时,它会正常工作

NOTE: While accessing class in react use className and not class

这是一个如何在 index.html

中添加 CDN 的示例

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: ''
    };

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({
      value: event.target.value
    });
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return ( 
    <div>
      <form onSubmit = {this.handleSubmit} >
        <label>
          Name:
        </label> 
        <input type = "text" className="form-control"
          value = {this.state.value}
          onChange = {this.handleChange}/> 
        <input type = "submit"
          value = "Submit" className="btn btn-primary"/>
     </form> 
   </div>
    );
  }
}

ReactDOM.render( < App / > , document.getElementById('root'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<div id='root'></div>


编辑:- 假设你根据 Official Installation 安装了 React

使用下面的文件结构

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/style/style.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>        
    <!-- And then your bundled js -->
  </head>
  <body>
    <div class="container"></div>
  </body>
  <script src="/bundle.js"></script>
</html>

index.js

import React from 'react';
import ReactDOM from 'react-dom';

import App from './components/app'; //Make sure it's the path to your App.js

ReactDOM.render(
    <App />
  , document.querySelector('.container'));

src/components/app.js

import React, { Component } from 'react';

export default class App extends Component {
  render() {
    return (
      <div>
        <form onSubmit = {this.handleSubmit} >
          <label>
            Name:
          </label> 
          <input type = "text" className="form-control"
            value = {this.state.value}
            onChange = {this.handleChange}/> 
          <input type = "submit"
          value = "Submit" className="btn btn-primary"/>
        </form> 
      </div>
    );
  }
}

检查一下,它应该可以工作。

关于html - 如何使用 bootstrap 和 reactjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47743653/

相关文章:

java - 使用 thymeleaf 和 bootstrap 未在验证表单上应用样式

twitter-bootstrap-4 - 如何让holder.js与bootstrap 4一起使用?

javascript - 当滚动条到达特定位置时动画 div

javascript - 为什么需要先显示图片才能让 jquery 图像放大镜工作?

if-statement - 渲染函数 ReactJS 中的 IF block

reactjs - 当scrollTop为零时,在渲染后 react 滚动位置

css - Bootstrap 4 网格 "cube"

css - 在液体 CSS 上居中一个 div 文本

javascript - 网页上使用 WebRTC 代替 UDP 流来删除 VLC 播放器

javascript - 无法在 ReactJs 中设置 null、getUserMedia 的属性 'srcObject'