使用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
相关推荐
Lee川4 小时前
Milvus 实战:当 RAG 遇上向量数据库,从"玩具 Demo"到"生产可用的"那一步
前端·数据库·人工智能
anOnion4 小时前
构建无障碍组件之Toolbar Pattern
前端·html·交互设计
惊鸿一博5 小时前
图标加载方式_zeroIcon_是否加前缀mdi
开发语言·前端·javascript
2501_940041745 小时前
前端工程化进阶:5个高交互与可视化项目提示词
前端
你很易烊千玺5 小时前
JS 异步 从零讲(大白话 + 真实场景 + 可运行案例)
前端·javascript·vue.js
why技术7 小时前
AI Coding开始进入第四个时代,我还没上车呢!
前端·人工智能·后端
大家的林语冰8 小时前
CSS 已死?DOM 性能黑洞!Pretext 排版革命让你在文本间跳舞,没有 DOM 也能纵享丝滑~
前端·javascript·css
vipbic8 小时前
我也该升级了,陪伴了我7年的博客
前端
Lee川8 小时前
RAG 实战:从一篇掘金文章出发,拆解检索增强生成的全链路
前端·人工智能·后端
Lee川8 小时前
MCP 高德地图实战:当 AI 学会使用工具,一个协议如何重塑大模型的行动边界
前端·人工智能·后端