Vite+React+TypeScript手?jǐn)]TodoList的項(xiàng)目實(shí)踐
布局與樣式
一個(gè)TodoList長(zhǎng)什么樣子相信無(wú)需多言:

上樣式: src/TodoList.css
.td-wrapper {
width: 700px;
margin: 0 auto;
}
.dp-wrapper {
width: 100%;
height: 40px;
display: flex;
margin-top: 10px;
}
.dp-wrapper input {
flex: 4;
height: 36px;
line-height: 36px;
text-indent: 10px;
font-size: 1rem;
}
.dp-wrapper button {
flex: 1;
height: 100%;
margin-left: 2px;
font-size: 1rem;
}
.dl-wrapper {
border: 1px solid gray;
margin-top: 5px;
}
.dl-wrapper li {
height: 40px;
line-height: 40px;
border-bottom: 1px solid gray;
}
.dl-wrapper li.done {
text-decoration: line-through;
}
.dl-wrapper li:last-child {
border-bottom: none;
}創(chuàng)建工程
npm init vite@latest
后續(xù)選擇:react + ts 添加必要文件,工程結(jié)構(gòu)如下:

定義全局?jǐn)?shù)據(jù)類(lèi)型
src/vite-env.d.ts
/// <reference types="vite/client" />
/* 代辦事項(xiàng)數(shù)據(jù)結(jié)構(gòu) */
interface TodoItem {
name: string,
done: boolean
}
/* 通用DOM事件處理器 */
type EventHandler = (e?: SyntheticEvent) => void
/* 處理函數(shù)定義:點(diǎn)擊提交按鈕 */
type UserInputHandler = (userInput: string, e?: SyntheticEvent) => void
/* 處理函數(shù)定義:點(diǎn)擊列表?xiàng)l目 */
type ImteClickHandler = (index: number, e?: SyntheticEvent) => void
/* 獲取指定Item的樣式名 */
type ItemClassNameGetter = (index: number) => string
/* 定義DataPicker組件的Props */
interface DataPickerProps {
onUserInput: UserInputHandler
}
/* 定義DataLister組件的Props */
interface DataListerProps {
list: TodoItem[],
onItemClick: ImteClickHandler,
getClassName: ItemClassNameGetter
}實(shí)現(xiàn)步驟
在App.tsx中加載TodoList:
import { useState } from 'react'
import './App.css'
import TodoList from './TodoList'
function App() {
return (
<div className="App">
<TodoList></TodoList>
</div>
)
}
export default App父組件TodoList具體實(shí)現(xiàn): src/TodoList.tsx
import React, { useState } from 'react'
import DataLister from './DataLister'
import DataPicker from './DataPicker'
/* 引入全局樣式 */
import "./TodoList.css"
export default function TodoList() {
/* 定義全局代辦事項(xiàng)列表 */
const [todoList, setTodoList] = useState([
{ name: "抽中華", done: false },
{ name: "喝劍南春", done: false },
{ name: "燙殺馬特", done: true },
])
/* 添加代辦事項(xiàng) */
const addTodoItem: UserInputHandler = (userInput: string) => {
setTodoList([
{ name: userInput, done: false },
...todoList
])
}
/* 切換代辦事項(xiàng)完成狀態(tài) */
const switchTodoitemState: ImteClickHandler = (index: number) => {
setTodoList(
todoList.map(
(item, i) => (
i !== index ? item : { ...item, done: !item.done }
)
)
)
}
/* 根據(jù)條目完成與否返回不同的樣式名 */
const getTodoitemClassName: ItemClassNameGetter = (index: number) => {
return todoList[index].done ? "done" : ""
}
/* 渲染 */
return (
<div className="td-wrapper">
<h3>TodoList</h3>
<DataPicker onUserInput={addTodoItem} />
<DataLister
list={todoList}
onItemClick={switchTodoitemState}
getClassName={getTodoitemClassName}
/>
</div>
)
}用戶(hù)輸入框組件實(shí)現(xiàn): src/DataPicker.tsx
import React, { SyntheticEvent, useState } from 'react'
export default function DataPicker({ onUserInput }: DataPickerProps) {
/* 定義響應(yīng)式數(shù)據(jù):用戶(hù)輸入的內(nèi)容 */
const [userInput, setUserInput] = useState("騷年請(qǐng)輸入你的悶響...")
/* 受控組件的雙向數(shù)據(jù)綁定 */
const onUserInputChange: EventHandler = (e: SyntheticEvent) => {
setUserInput((e.target as HTMLInputElement).value)
}
/* 處理提交按鈕點(diǎn)擊事件 */
const onSubmit: EventHandler = (e: SyntheticEvent) => {
/* 將用戶(hù)的輸入告知父組件,由父組件自行定奪如何處置之 */
onUserInput(userInput)
// 清空用戶(hù)輸入
setUserInput("")
}
/* 渲染 */
return (
<div className='dp-wrapper'>
<input type="text" value={userInput} onChange={onUserInputChange} />
<button onClick={onSubmit}>提交</button>
</div>
)
}列表展示組件實(shí)現(xiàn): src/DataLister.tsx
export default function DataLister({ list, onItemClick, getClassName }: DataListerProps) {
return (
<div className='dl-wrapper'>
<ul>{
list.map(
(item: TodoItem, index: number) => (
<li
key={index + item.name}
className={getClassName(index)}
onClick={() => {
/* 告訴父組件第幾個(gè)item被點(diǎn)擊了 具體如何處置由父元素自行決定 */
onItemClick(index)
}}>
{item.name}
</li>
)
)
}</ul>
</div>
)
}源碼地址
git clone https://gitee.com/steveouyang/todolist-vite-react-ts.git
到此這篇關(guān)于Vite+React+TypeScript手?jǐn)]TodoList的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)Vite React TypeScript TodoList內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于React實(shí)現(xiàn)一個(gè)todo打勾效果
這篇文章主要為大家詳細(xì)介紹了如何基于React實(shí)現(xiàn)一個(gè)todo打勾效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
基于Node的React圖片上傳組件實(shí)現(xiàn)實(shí)例代碼
本篇文章主要介紹了基于Node的React圖片上傳組件實(shí)現(xiàn)實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
使用Electron構(gòu)建React+Webpack桌面應(yīng)用的方法
本篇文章主要介紹了使用Electron構(gòu)建React+Webpack桌面應(yīng)用的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
React?高階組件與Render?Props優(yōu)缺點(diǎn)詳解
這篇文章主要weidajai?介紹了React?高階組件與Render?Props優(yōu)缺點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
antd?3.x?Table組件如何快速實(shí)現(xiàn)虛擬列表詳析
這篇文章主要給大家介紹了關(guān)于antd?3.x?Table組件如何快速實(shí)現(xiàn)虛擬列表的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用antd具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-11-11

