美文网首页
使用react hooks编写一个todo list

使用react hooks编写一个todo list

作者: mudssky | 来源:发表于2021-07-27 18:00 被阅读0次

实现了添加todo和删除todo,还有用localStorage持久化的功能.

样式直接用的tailwind css 编写的,只用在className里面写类名,省事不少.

// import react from 'react'

import { useEffect, useState } from "react"

interface item{
     key: string;
    value: string;
}

export default function TodoList() {
    const [inputValue, setInputValue] = useState('')
    const [itemList, setItemList] = useState(Array<item>())
    // 挂载时执行,载入itemList
    useEffect(() => {
        // effect
        const todos=localStorage.getItem('todos')
        if (todos) {
            setItemList(JSON.parse(todos))
        }
    }, [])

    // 更新时执行,保存todolist
    useEffect(() => {
        localStorage.setItem('todos',JSON.stringify(itemList))
    }, [itemList])
    return (
        <div className="flex flex-col  max-w-lg m-auto">
            <h1 className='text-center'>TodoList</h1>
            <div>
            <input type="text" className="w-full ring-2 ring-blue-400 rounded-md outline-none mt-1 focus:ring-pink-300" id=""
                onChange={(e) => { setInputValue(e.target.value) }}
                    value={inputValue}
                    onKeyDown={(e) => {
                        if (e.key === 'Enter') {
                            const newList = itemList.slice()
                            newList.push({key: new Date().getTime()+inputValue, value: inputValue })
                            setItemList(newList)
                            setInputValue('')
                        }}}
                />
            </div>
            <ul className="mt-2">{
                itemList.map((i) =>
                <div className="flex mt-2">
                    <li key={i.key} className="bg-blue-100 flex-auto">
                        {i.value}
                    </li>
                        <button className="w-1/12 bg-red-400" onClick={() => {
                            const newList = itemList.filter((x) => x !== i)
                            setItemList(newList)
                        }}>del</button>
                </div>
                )}
            </ul>
        </div>
    )
}


相关文章

  • 使用react hooks编写一个todo list

    实现了添加todo和删除todo,还有用localStorage持久化的功能. 样式直接用的tailwind cs...

  • React hooks(钩子)

    React hooks(钩子) React hooks 是React 16.8中的新增功能。它们使您无需编写类即可...

  • React Hooks

    React Hooks它可以让我们不编写class的情况下使用state以及其他的React特性; useSta...

  • React 16.8发布了

    React 16.8 终于带来了稳定版的 Hooks。 什么是 hooks? hooks 可以让你在不编写类的情况...

  • Hooks

    简介 Hooks 是 React 16.8 的新增特性。它可以让你在不编写 class 的情况下使用 state ...

  • React组件化(三)-Hooks

    Hooks是React16.8新增的,它可以让你不编写class的情况下使用state及其他React特性。Hoo...

  • React Hooks

    Hooks 是 React 16.8 的新增特性。它可以让你在不编写 class 的情况下使用 state 以及其...

  • react todo list

  • useTypescript-React Hooks和TypeSc

    引言 React v16.8 引入了 Hooks,它可以让你在不编写 class 的情况下使用 state 以及其...

  • hooks

    hooks就是函数组件,可以在不编写 class 的情况下使用 state 以及其他的 React 特性。

网友评论

      本文标题:使用react hooks编写一个todo list

      本文链接:https://www.haomeiwen.com/subject/mlbomltx.html