使用React Bootstrap搭建网页界面

先安装node,如果有旧版本的node需要先卸载

bash 复制代码
sudo apt-get remove nodejs
sudo apt-get autoremove

安装node,官网为https://deb.nodesource.com/

按照提示安装最新版

快速搭建 Vite 项目的命令,xxx为项目名

bash 复制代码
npm create vite xxx

安装react-bootstrap

bash 复制代码
npm install react-bootstrap bootstra

要想使用bootstrap需要修改以下文件

  1. 删除app.css与index.css,将app.jsx中的import './App.css'删除

  2. 将main.jsx中的import App from './App.jsx'与import './App.css'删除,再添加 import "bootstrap/dist/css/bootstrap.css"

  3. 再创建components文件夹存放自己写的代码

可以参考react-bootstrap官网的示例

https://react-bootstrap.github.io/docs/getting-started/introduction

在components中创建mainpage.jsx文件

添加react-bootstrap官网中的一个小例子

bash 复制代码
import Alert from 'react-bootstrap/Alert';

function BasicExample() {
  return (
    <>
      {[
        'primary',
        'secondary',
        'success',
        'danger',
        'warning',
        'info',
        'light',
        'dark',
      ].map((variant) => (
        <Alert key={variant} variant={variant}>
          This is a {variant} alert---check it out!
        </Alert>
      ))}
    </>
  );
}

export default BasicExample;

修改App.jsx的内容

bash 复制代码
import { useState } from 'react'

import BasicExample from './components/mainPage'

function App() {
  const [count, setCount] = useState(0)

  return (
    <>

      <BasicExample />

    </>
  )
}

export default App

然后运行代码

bash 复制代码
npm run dev

点击local链接,跳转到浏览器,查看效果

如果设计好页面后可以直接构建出生产环境代码

bash 复制代码
npm run build
相关推荐
cxxcode19 分钟前
Vite 热更新(HMR)原理详解
前端
HelloReader29 分钟前
Tauri 架构从“WebView + Rust”到完整工具链与生态
前端
Bigger42 分钟前
告别版本焦虑:如何为 Hugo 项目定制专属构建环境
前端·架构·go
代码匠心2 小时前
AI 自动编程:一句话设计高颜值博客
前端·ai·ai编程·claude
_AaronWong4 小时前
Electron 实现仿豆包划词取词功能:从 AI 生成到落地踩坑记
前端·javascript·vue.js
cxxcode4 小时前
I/O 多路复用:从浏览器到 Linux 内核
前端
用户5433081441944 小时前
AI 时代,前端逆向的门槛已经低到离谱 — 以 Upwork 为例
前端
JarvanMo4 小时前
Flutter 版本的 material_ui 已经上架 pub.dev 啦!快来抢先体验吧。
前端
恋猫de小郭4 小时前
AI 可以让 WIFI 实现监控室内人体位置和姿态,无需摄像头?
前端·人工智能·ai编程
哀木4 小时前
给自己整一个 claude code,解锁编程新姿势
前端