next.js博客搭建_六一卡通风格的登录注册(第二步)


yma16-logo

⭐前言

大家好,我是yma16,本期给大家分享next项目搭建卡通风的登录注册。
该系列的往期文章
博客搭建_初始化next项目
登录注册的交互效果如下:
背景图为卡通的动画,符合六一的风格!
next-login-register

react 函数组件风格介绍

React 函数组件是一个简单的 React 组件,是一个纯 JavaScript 函数,用于定义组件的结构和行为。相比于 Class 组件,函数组件的代码量更少,结构更清晰,也更易于测试和维护。下面是一些函数组件的风格介绍:

  1. 使用箭头函数定义函数组件:
const MyComponent = () => {
  return <div>Hello World</div>;
}
  1. 使用 props 传递数据:
const MyComponent = (props) => {
  return <div>Hello {props.name}</div>;
}

// 使用组件时传递 props
<MyComponent name="World" />
  1. 使用解构赋值获取 props:
const MyComponent = ({ name }) => {
  return <div>Hello {name}</div>;
}

// 使用组件时传递 props
<MyComponent name="World" />
  1. 通过 useState Hook 管理组件状态:
import React, { useState } from "react";

const MyComponent = () => {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}
  1. 使用 useEffect Hook 处理副作用:
import React, { useState, useEffect } from "react";

const MyComponent = () => {
  const [count, setCount] = useState(0);

  useEffect(() => {
    document.title = `Count: ${count}`;
  }, [count]);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

这些风格可以帮助您更加简洁和有效地编写 React 函数组件。

⭐ 登录界面搭建

页面元素:

  1. 登录的表单包括账号和密码
  2. 没有账号可以跳转到注册页面的按钮
  3. 表单校验

💖 登录效果

next-login
定义onSubmit 提交事件去校验输入框的内容,jumpAction的函数跳转到注册页面,添加reset重置form表单的方法
代码实现如下:

import React from 'react';
import { Form, Input, Button, MessagePlugin,Link } from 'tdesign-react';
import { DesktopIcon, LockOnIcon } from 'tdesign-icons-react';
import {loginAction} from "../../service/user/userApi"
import { useRouter } from 'next/router'

const { FormItem } = Form;

export default function BaseForm() {
    const router = useRouter()
    const rules=[
        { required: true, message: '不能为空', type: 'warning' }
    ]
    const onSubmit = (e:any) => {
        console.log(e);
        if (e.validateResult === true) {

            loginAction({
                name:e.fields?.account,
                password:e.fields?.password
            }).then(res=>{
                console.log('res',res)
                MessagePlugin.info('登录成功');
            }).catch(r=>{
                MessagePlugin.error('登录失败\n'+JSON.stringify(r));
            })
        }
    };

    const jumpAction=()=>{
        router.push('/views/sys/register')
    }

    const onReset = (e) => {
        console.log(e);
        MessagePlugin.info('重置成功');
    };

    return (
        <div className={"login-box"}>
        <div className={"login-container"}>
            <div style={{width:'100%',textAlign:'center',marginBottom:'20px',fontWeight:'bold'}}>
                登录
            </div>
        <div style={{ width: 350 }}>
            <Form statusIcon={true} onSubmit={onSubmit} onReset={onReset} colon={true} labelWidth={0}>
                <FormItem name="account" rules={rules}>
                    <Input clearable={true} prefixIcon={<DesktopIcon />} placeholder="请输入账户名" />
                </FormItem>
                <FormItem name="password" rules={rules}>
                    <Input type="password" prefixIcon={<LockOnIcon />} clearable={true} placeholder="请输入密码"
                    />
                </FormItem>
                <FormItem>
                    <Button theme="primary" type="submit" block>
                        登录
                    </Button>
                </FormItem>
            </Form>
            <div style={{width:'100%',textAlign:'center',marginTop:'10px'}} onClick={jumpAction}>
                没有账号?<Link theme="primary">前往注册</Link>
            </div>
        </div>
        </div>
        </div>
    );
}

⭐ 注册界面搭建

思路分解

页面元素:

  1. 注册的表单包括账号、密码和确认密码
  2. 有账号可以跳转到登录页面
  3. 表单校验

registerAction注册对接后端的接口,jumpAction跳转到登录的方法,reset重置表单的方法。
注册页面的代码如下:

import React from 'react';
import { Form, Input, Button, MessagePlugin,Link } from 'tdesign-react';
import { DesktopIcon, LockOnIcon } from 'tdesign-icons-react';
import {registerAction} from "../../service/user/userApi"
import { useRouter } from 'next/router'

const { FormItem } = Form;

export default function BaseForm() {
    const [form]=Form.useForm()
    const router = useRouter()
    const rules=[
        { required: true, message: '不能为空', type: 'warning' }
    ]
    const pwdRules=[
        { required: true, message: '不能为空', type: 'warning' },
        { validator: asyncValidatePwd, message: '密码不一致', type: 'warning', trigger: 'blur' },
    ]

    function asyncValidatePwd(val) {
        // 密码不一致的校验
        return new Promise((resolve) => {
            setTimeout(() => {
                if (val === form.getFieldValue('password')) {
                    resolve(true);
                } else {
                    resolve(false);
                }
            }, 1000);
        });
    }
    const onSubmit = (e:any) => {
        console.log(e);
        if (e.validateResult === true) {

            registerAction({
                name:e.fields?.account,
                password:e.fields?.password
            }).then(res=>{
                console.log('res',res)
                if(res.data.code){
                    MessagePlugin.info('注册成功');
                }
                else{
                    MessagePlugin.error('注册失败\n'+res.data.msg);
                }

            }).catch(r=>{
                MessagePlugin.error('注册失败\n'+JSON.stringify(r));
            })
        }
    };

    const jumpAction=()=>{
        router.push('/views/sys/login')
    }

    const onReset = (e) => {
        console.log(e);
        MessagePlugin.info('重置成功');
    };

    return (
        <div className={"login-box"}>
            <div className={"login-container"}>
                <div style={{width:'100%',textAlign:'center',marginBottom:'20px',fontWeight:'bold'}}>
                    注册
                </div>
                <div style={{ width: 350 }}>
                    <Form statusIcon={true} onSubmit={onSubmit} onReset={onReset} colon={true} labelWidth={0} form={form}>
                        <FormItem name="account" rules={rules}>
                            <Input clearable={true} prefixIcon={<DesktopIcon />} placeholder="请输入账户名" />
                        </FormItem>
                        <FormItem name="password" rules={rules}>
                            <Input type="password" prefixIcon={<LockOnIcon />} clearable={true} placeholder="请输入密码"
                            />
                        </FormItem>
                        <FormItem name="confirmPwd" rules={pwdRules}>
                            <Input type="confirmPwd" prefixIcon={<LockOnIcon />} clearable={true} placeholder="请确认密码"
                            />
                        </FormItem>
                        <FormItem>
                            <Button theme="primary" type="submit" block>
                                注册
                            </Button>
                        </FormItem>
                    </Form>
                    <div style={{width:'100%',textAlign:'center',marginTop:'10px'}} onClick={jumpAction}>
                        已有账号?<Link theme="primary">前往登录</Link>
                    </div>
                </div>
            </div>
        </div>
    );
}

💖 注册效果

next-register

⭐ 结束

博客的卡通风格登录注册到这结束,如有不足欢迎指出!
💖 感谢你的阅读 💖
我们下篇博客见!
cartoon-background

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yma16

感谢支持!共勉!

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

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

打赏作者

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

抵扣说明:

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

余额充值