登录页面。

import React, { useEffect,useState } from 'react'

import axios from 'axios'

import { render } from '@testing-library/react';

import { useHistory } from 'react-router-dom'


 

export default function () {

    const [list,setlist] = useState([]);

    const [username,setusername] = useState('')

    const [password,setpassword] = useState('')

    const [tel,settel] = useState('')

    const history = useHistory()

   

    useEffect(()=>{

        axios.get('./data/user.json').then(({data})=>{

            setlist(data)

        })

    },[])

    // 获取表单中的数据

    const login = () => {

        let foundMatch = false;

        const copylist = [...list]

        copylist.forEach((item) => {

            if(username === item.username && password === item.password && tel === item.tel){

                foundMatch = true

            }

        });

        if (foundMatch){

            sessionStorage.token = '123'

            history.push('/list')

        }else{

            alert('内容不正确')

        }

    }



 

    return (

        <div>

            {/* <form onSubmit={handleSubmit}> */}

                <input type="text" placeholder='请输入用户名' value={username} onChange={(event) => setusername(event.target.value)} />  <br />

                <input type="password" placeholder='请输入密码' value={password} onChange={(event) => setpassword(event.target.value)}   /> <br />

                <input type="text" placeholder='请输入手机号' value={tel} onChange={(event) => settel(event.target.value)} />  <br />

                <button type='submit' onClick={ login }>登录</button>

            {/* </form> */}

        </div>

    )

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值