一:使用Vite创建React项目

二:React中组件使用

import React, { Component, useState } from "react";

//使用class定义组件

export default class Modet extends Component<any,any> {
// class组件中可以使用构造器
constructor(props: any) {
super(props);
//定义状态
this.state = { list: { id: 1, name: "小屋" }, list2: [] }
}
//(生命周期)创建时的函数,一般用于初始化数据
componentDidMount(): void { }
//(生命周期)数据修改时的函数,用于监听数据变化
componentDidUpdate(): void { }
//(生命周期)组件卸载时的函数,用于清除数据,如定时器之类的
componentWillUnmount(): void { }
render() {
return (
// 幽灵标签
<>
{/* bind传参 */}
{/* <div onClick={this.方法名称.bind(this, 当前对象)}></div> */} {/* map使用 */}
{
this.state.list2.map((list:any,i:any)=>{
<div key={i}>{list}</div>
})
} {/* React中for循环读取图片的方式 */}
{
/* {this.state.list2.forum_img=>图片集合逗号分隔后在循环 ,Vue中好像不可以这样写*/
this.state.list2.forum_img.split(',').map((imageName: any, index: any) => (
<img src={"./src/assets/img/" + imageName} alt="imageName" key={index} />
))
} </>
)
}
} //使用函数定义组件
export function Modet2() {
// 函数组件中定义有状态数据
let [user_pwd, setUserPwd] = useState();
//修改数据方法
function LoginSet(e: any) {
// e是当前传过来的数据可以输出看看
setUserPwd(e.target.value);
}
return(
<>
<div onClick={LoginSet}></div>
</>
)
}

三:在App.tsx中声明

import { Routes, BrowserRouter as Router, Route } from 'react-router-dom'
import Index from './component'
import { Login } from './component/login'
function App() {
return (
<Router>
<Routes>
<Route path='/' element={<Login />}></Route>
<Route path='/home' element={<Index />}></Route>
</Routes>
</Router>
)
} export default App

四:最后在main.ts中引用并且挂载App.tsx

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
//import App from '../src/component/text'
import './index.css' ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)

五:路由使用

1、安装路由

npm i react-router-dom

2、引入路由

import {
BrowserRouter as Router,--也可以直接使用BrowserRouter
NavLink,--可以进行选中判断
Route, Routes
} from 'react-router-dom'

代码:

App.tsx

import './App.css'
import {
BrowserRouter as Router,
NavLink,
Route, Routes
} from 'react-router-dom' import February from './component/february'
import March from './component/march'
import Times from './component/february/index2'
import Btn from './component/february/index'
import Input from './component/february/index3'
import Paging from './component/february/index4'
import Model from './component/march/march1'
import Change from './component/march/march2'
import { Sloting } from './component/march/march3'
import { Stateful } from './component/march/march4'
import { StatefulWork } from './component/march/march5'
import WordSix from './component/march/march6'
import Product from './component/march/march7'
import Button from './component/march/march8'
import Effect from './component/march/march9'
import Hooks from './component/march/march10'
import Context from './component/march/march11'
import VModel from './component/february/index5'
import { Reducer } from './component/march/march12'
import { Want } from './component/february/index6' function App() {
return (
<Router>
<NavLink className={({ isActive }) => isActive ? 'active' : ''} to="/february">2023-02-28</NavLink>
<NavLink className={({ isActive }) => isActive ? 'active' : ''} to="/march">2023-03-01</NavLink>
<Routes>
<Route path='/' element=''></Route>
<Route path='february' element={<February />}>
{/* index 默认显示 */}
{/* <Route index element={<Times />} /> */}
<Route path='times' element={<Times />} />
<Route path='btn' element={<Btn />} />
<Route path='input' element={<Input />} />
<Route path='paging' element={<Paging />} />
<Route path='vmodel' element={<VModel />} />
<Route path='text' element={<Want />} />
</Route>
<Route path='march' element={<March />}>
<Route path='model' element={<Model />}></Route>
<Route path='change' element={<Change />}></Route>
<Route path='slot' element={<Sloting />}></Route>
<Route path='stateful' element={<Stateful />}></Route>
<Route path='statefulWork' element={<StatefulWork />}></Route>
<Route path='work' element={<WordSix />}></Route>
<Route path='product' element={<Product />}></Route>
<Route path='btn' element={<Button />}></Route>
<Route path='effect' element={<Effect />}></Route>
<Route path='hooks' element={<Hooks />}></Route>
<Route path='context' element={<Context />}></Route>
<Route path='reducer' element={<Reducer />}></Route>
</Route>
{/* Navigate重定向 */}
{/* to相当于push,有历史记录,可以后退 */}
{/* replace没有历史记录 */}
{/* <Route path='/march' element={<Navigate to='/home' />}></Route> */}
</Routes>
</Router>
)
} export default App

子路由,March

import React from "react";
import { NavLink, Outlet } from "react-router-dom";
import '../css/index.css'
export default function March() {
return (
<>
<h3>三月份作业</h3>
<div className="div_title">
<NavLink className={({isActive})=> isActive?'title_active':''} to={"model"}>模态框</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"change"}>点击切换</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"slot"}>插槽</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"stateful"}>状态组件</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"statefulWork"}>状态组件作业</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"work"}>作业</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"product"}>父传子案例</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"btn"}>useState案例</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"effect"}>useEffect案例</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"hooks"}>Hooks案例</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"context"}>Context案例</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={"reducer"}>Reducer案例</NavLink>
</div>
<div>
<Outlet />
</div>
</>
)
}

子路由,February

import React from "react";
import { NavLink, Outlet } from "react-router-dom";
import '../css/index.css'
export default function February() {
return (
<>
<h3>二月份作业</h3>
<div className="div_title">
<NavLink className={({isActive})=> isActive?'title_active':''} to={'times'}>动态显示当前时间</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={'btn'}>单击显示隐藏</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={'input'}>数据双向绑定</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={'paging'}>分页插件</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={'vmodel'}> v-model原生</NavLink>
<NavLink className={({isActive})=> isActive?'title_active':''} to={'text'}> 深拷贝浅拷贝</NavLink>
</div>
<div>
{/* 占位符,让当前组件显示在占位符的位置 */}
<Outlet />
</div>
</>
)
}

React框架使用的更多相关文章

  1. 当react框架遇上百度地图

      百度地图官方文档的使用指导是这样说的:在页面中引入<script type="text/javascript" src="http://api.map.baid ...

  2. 谈谈出入React框架踩过的坑

    1 在JSX的元素中写入内联样式,例如<div style={"color:blue"}></div> 报错:warning:Style prop valu ...

  3. 【react】当react框架遇上百度地图

      百度地图官方文档的使用指导是这样说的:在页面中引入<script type="text/javascript" src="http://api.map.baid ...

  4. D3.js(v3)+react框架 基础部分之数据绑定及其工作过程与绑定顺序

    数据绑定: 将数据绑定到Dom上,是D3最大的特色.d3.select和d3.selectAll返回的元素的选择集.选择集上是没有数据的. 数据绑定就是使被选择元素里“含有”数据. 相关函数有两个: ...

  5. 认识React框架

    在大厂面试的时候被问会不会React框架几乎是必须的,可见React框架在现在前端市场的份额.所以说学习React框架的必要性. react框架起源于Facebook的内部项目,因为对市场上的Java ...

  6. 搭建 webpack + react 框架爬坑之路

    由于工程实践需要搭一个 webpack + react 框架,本人刚开始学,就照b站上的react黑马视频做,爬过无数个坑...希望读者能引以为戒.我的是macos系统 https://www.bil ...

  7. 【案例分享】在 React 框架中使用 SpreadJS 纯前端表格控件

    [案例分享]在 React 框架中使用 SpreadJS 纯前端表格控件 本期葡萄城公开课,将由国电联合动力技术有限公司,资深前端开发工程师——李林慧女士,与大家在线分享“在 React 框架中使用 ...

  8. react框架下,在页面内加载显示PDF文件,关于react-pdf-js的使用注意事项

    react框架下,在页面内加载显示PDF文件,关于react-pdf-js的使用注意事项 之前做了一个需求,在注册账号的时候,让用户同意服务条款, 服务条款是一个PDF文件, 这就需要在react内加 ...

  9. React框架随笔

    React框架随笔 现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了 ...

  10. React框架概述

    一.React框架概述 官网:https://reactjs.org/       最新版V16.10 中文网:https://zh-hans.reactjs.org/ 中文社区网:https://r ...

随机推荐

  1. linux行去重的一种方法

    awk '{x[$0]++}END{for(i in x)print i}' newsub > newsub2// orcat oldword| awk  -F "\t" ' ...

  2. DNS解析原理(www.baidu.com)

    QueryDns,py程序运行问题解决 关于远程访问数据库问题 这个我用NAVICAT或者是python程序连接都连不上他那个数据库(可能是数据库设定的权限没有开启?) 这个程序真的跑不起来,考虑自己 ...

  3. 在 vuex 中建立一个 permission.js 文件用于合并静态和动态的路由规则

    结果会获取完整的用户的路由规则 // 新建一个vuex模块来专门处理权限相关操作 import { constantRoutes, asyncRoutes } from "@/router& ...

  4. CentOS查看已安装的服务与卸载服务。。

    1:使用rpm查看, rmp -qa | grep servername rpm -qa 查看以安装的所有服务,grep过滤我们需要看的服务. 2:使用yum查看<此命令恕在下未能完全理解,可能 ...

  5. HDFS Property列表,适用于Hadoop 2.4以上 。

    Property列表链接:http://hadoop.apache.org/docs/r2.4.1/hadoop-project-dist/hadoop-hdfs/hdfs-default.xml 以 ...

  6. java锁之初相见

    废话不多说,先上锁的分类图 1.乐观锁&悲观锁 悲观锁 悲观锁认为自己在使用数据的时候一定有别的线程来修改数据,因此在获取数据的时候会先加锁,把别的线程阻塞住,最终确保数据不会被别的线程修改. ...

  7. 文件上传 upload-labs Pass-18 条件竞争

    Pass-18 条件竞争 审计源码 $is_upload = false; $msg = null; if(isset($_POST['submit'])){ $ext_arr = array('jp ...

  8. 【QCustomPlot】简介

    说明 使用 QCustomPlot 绘图库辅助开发时整理的学习笔记. 目录 说明 1. 库简介 2. 库的官网链接 3. 库的帮助文档 4. 库的下载地址 5. 库的版本号说明 6. 库的 Git 地 ...

  9. Linux & 标准C语言学习 <DAY4>

    一.数据类型     为什么要对数据进行分类         1.现实中的数据就是自带类别属性的         2.对数据进行分类可以节约内存存储空间.提高运行速度     C语言中数据分为两大类别 ...

  10. MySQL 高级查询截取分析

    慢日志分析流程如下:[1]观察(至少跑一天),看看生产慢 SQL 情况.[2]开启慢查询日志,设置阈值,比如对超过5秒的 SQL 语句进行抓取.[3]explain+慢 SQL 分析.[4]show ...