React框架使用
一:使用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框架使用的更多相关文章
- 当react框架遇上百度地图
百度地图官方文档的使用指导是这样说的:在页面中引入<script type="text/javascript" src="http://api.map.baid ...
- 谈谈出入React框架踩过的坑
1 在JSX的元素中写入内联样式,例如<div style={"color:blue"}></div> 报错:warning:Style prop valu ...
- 【react】当react框架遇上百度地图
百度地图官方文档的使用指导是这样说的:在页面中引入<script type="text/javascript" src="http://api.map.baid ...
- D3.js(v3)+react框架 基础部分之数据绑定及其工作过程与绑定顺序
数据绑定: 将数据绑定到Dom上,是D3最大的特色.d3.select和d3.selectAll返回的元素的选择集.选择集上是没有数据的. 数据绑定就是使被选择元素里“含有”数据. 相关函数有两个: ...
- 认识React框架
在大厂面试的时候被问会不会React框架几乎是必须的,可见React框架在现在前端市场的份额.所以说学习React框架的必要性. react框架起源于Facebook的内部项目,因为对市场上的Java ...
- 搭建 webpack + react 框架爬坑之路
由于工程实践需要搭一个 webpack + react 框架,本人刚开始学,就照b站上的react黑马视频做,爬过无数个坑...希望读者能引以为戒.我的是macos系统 https://www.bil ...
- 【案例分享】在 React 框架中使用 SpreadJS 纯前端表格控件
[案例分享]在 React 框架中使用 SpreadJS 纯前端表格控件 本期葡萄城公开课,将由国电联合动力技术有限公司,资深前端开发工程师——李林慧女士,与大家在线分享“在 React 框架中使用 ...
- react框架下,在页面内加载显示PDF文件,关于react-pdf-js的使用注意事项
react框架下,在页面内加载显示PDF文件,关于react-pdf-js的使用注意事项 之前做了一个需求,在注册账号的时候,让用户同意服务条款, 服务条款是一个PDF文件, 这就需要在react内加 ...
- React框架随笔
React框架随笔 现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了 ...
- React框架概述
一.React框架概述 官网:https://reactjs.org/ 最新版V16.10 中文网:https://zh-hans.reactjs.org/ 中文社区网:https://r ...
随机推荐
- Matlab %陆
第六章 MATLAB IN ENGINEERING Polynomial Differentiation多项式微分 %幂级数 f(x) = x^3-2x-5; p = [1 0 -2 -5] %自 ...
- VMware linux 网络设置
控制面板\所有控制面板项\网络连接 1.选择 VMware Virtual Ethernet Adapter for VMnet8 网卡 ->属性-->网络 2.勾选 -> VMw ...
- 如何优化MySQL
1.MySQL数据库作发布系统的存储,一天五万条以上的增量,预计运维三年,怎么优化? a. 设计良好的数据库结构,允许部分数据冗余,尽量避免join查询,提高效率.b. 选择合适的表字段数据类型和存储 ...
- kubeadm部署单master Kuberntes集群
本文参考kubernetes docs 使用kubeadm创建single master的Kuberntes集群 虚机两台Centos75 Kubernetes Yum Repo采用国内阿里源 版本 ...
- VBA中的结构体
结构体必须放在"模块"中: Type Org tag As String person As New Collection End Type 使用: Sub testType() ...
- Class 'dmstr\web\AdminLteAsset' not found
Yii2出现 Class 'dmstr\web\AdminLteAsset' not found 报错 1.检查下是不是vendor从其他地方复制过来的 2.检查根目录composer.json 中 ...
- bat将多个文件夹下内容合并到一个文件夹下
for /f "delims=" %%p in ('dir /b/ad') do copy %%p\*.* d:\all\ pause 目标文件夹 d:\all\ 最好不用中文目录
- mock数据规则
Mock数据规则 随机生成100条内的list数据 let Mock = require("mockjs"); let basicData = Mock.mock({ " ...
- Linux 系统设置
ubuntu下使用PageUp/PageDown快速翻出历史命令 #vim/etc/inputrc 解除两行注视后重启终端 "\e[5~": history-search-back ...
- 微信小程序-顶部下拉菜单实现
最近写的小程序里面需要实现顶部下拉菜单的效果,做一个过滤操作,但是没有找到相关组件,所以动手写了一个. 先看一下这拙劣的效果叭~ 下面就直接看具体实现了嗷! index.wxml <view c ...