定义数据模型

import React, { useState, useRef, useEffect } from 'react'
import { nanoid } from 'nanoid'
export default function index ()
{
    useEffect(() =>
    {
        document.onkeydown = function (e)
        {
            if (e.keyCode == 13)  // 貌似 keyCode 被弃用了 有点小 bug
            {
                addPerson()
            }
        }
    }, [])
    const nameNode = useRef()
    const ageNode = useRef()
    const [DataModel, SetDataModel] = useState({
        // 定义数据模型
        height: '100vh',
        button: '不要背景了',
        backgroundColor: 'pink',
        list: [{ name: '羽神', age: 18, sex: '女' }],
    })
    const ChangeBackgroundColor = () =>
    {
        function getRandomIntInclusive (min, max)
        {
            min = Math.ceil(min);//返回小于一个数的最大整数 即一个数向下取整后的值
            max = Math.floor(max); //返回小于一个数的最大整数 即一个数向下取整后的值
            return Math.floor(Math.random() * (max - min + 1)) + min;
        } //含最大值,含最小值 

        let backgroundColor = DataModel.backgroundColor
        let height = DataModel.height == '100vh' ? '300px' : '100vh'

        switch (getRandomIntInclusive(1, 9))
        {
            case 1:
                backgroundColor = '#D24D57'
                break
            case 2:
                backgroundColor = '#26A65B'
                break
            case 3:
                backgroundColor = '#EB7347'
                break
            case 4:
                backgroundColor = '#2C3E50'
                break
            case 5:
                backgroundColor = '#84AF9B'
                break
            case 6:
                backgroundColor = '#FC9D99'
                break
            case 7:
                backgroundColor = '#AEDD81'
                break
            case 8:
                backgroundColor = '#00CCFF'
                break
            case 9:
                backgroundColor = 'pink'
                break
        }
        SetDataModel({ ...DataModel, backgroundColor, height, button: '不要背景了' })
    }
    const RemoveBackground = () =>
    {
        if (DataModel.button === '不要背景了')
        {
            if (window.confirm('真的不要我了吗'))
            {
                let { backgroundColor, ...NewDataModel } = DataModel  // 利用结构赋值 在不影响原变量的情况下删除一个且返回一个新的变量 
                SetDataModel({ ...NewDataModel, button: '我消失了' })
            }
        }
    }
    const change = (e) =>
    {
        const { value } = e.target
        SetDataModel({ ...DataModel, text: value })  // ES6 给 DataModel 添加一个属性
    }
    const addPerson = async () =>
    {
        try
        {
            const name = nameNode.current.value
            const age = ageNode.current.value
            let reg = /^\s*$/g
            if (!await nameNode.current.value)
            {
                return alert('名字都不会写吗')
            }
            if (!await ageNode.current.value)
            {
                return alert('赶紧输入年龄啊')
            }
            if (!reg.test(age) && !reg.test(age))
            {
                const personObj = { id: Boolean(Math.round(Math.random())), name, age }  // 随机生成 真假~
                SetDataModel({ ...DataModel, list: [...DataModel.list, personObj] })
                console.log(name);
                nameNode.current.value = null
                ageNode.current.value = null
            }
        } catch (err)
        {
            console.log(err);
        }
    }
    return (
        <div style={{ height: DataModel.height, backgroundColor: DataModel.backgroundColor, }}>
            <div style={{ textAlign: 'center' }}>
                <h1 >您输入的内容是:{DataModel.text}</h1>
            </div>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', flexWrap: 'wrap' }}>
                <input type="text" onChange={change} />
                <button style={{ border: '1', borderColor: 'red', margin: 10 }} onClick={ChangeBackgroundColor}>切换背景颜色</button>
                <button style={{ border: '1', borderColor: 'red', margin: 10 }} onClick={RemoveBackground}>{DataModel.button}</button>
            </div>
            <div style={{ textAlign: 'center' }}>
                <h1>当前的学生信息:</h1>
                {
                    DataModel.list.map(data =>
                    {
                        return (
                            <div key={nanoid()}>
                                <h2>姓名:{data.name},年龄:{data.age}性别:{data.sex ? data.sex : data.id ? '男' : '女'}</h2>
                            </div>
                        )
                    })
                }
                <input type="text" ref={nameNode} placeholder="输入名字" />
                <input type="number" ref={ageNode} placeholder="输入年龄" />
                <button onClick={addPerson}>添加</button>
            </div>
        </div>
    )
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臧小川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值