使用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
相关推荐
于慨1 天前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
石小石Orz1 天前
油猴脚本实现生产环境加载本地qiankun子应用
前端·架构
从前慢丶1 天前
前端交互规范(Web 端)
前端
CHU7290351 天前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序
GISer_Jing1 天前
Page-agent MCP结构
前端·人工智能
王霸天1 天前
💥别再抄网上的Scale缩放代码了!50行源码教你写一个永不翻车的大屏适配
前端·vue.js·数据可视化
小领航1 天前
用 Three.js + Vue 3 打造炫酷的 3D 行政地图可视化组件
前端·github
@大迁世界1 天前
2026年React大洗牌:React Hooks 将迎来重大升级
前端·javascript·react.js·前端框架·ecmascript
PieroPc1 天前
一个功能强大的 Web 端标签设计和打印工具,支持服务器端直接打印到局域网打印机。Fastapi + html
前端·html·fastapi
悟空瞎说1 天前
深入 Vue3 响应式:为什么有的要加.value,有的不用?从设计到源码彻底讲透
前端·vue.js