React实现登录表单
程序员文章站
2022-07-02 23:47:25
...
作为一个Vue用户,是时候扩展一下React了,从引入antd、配置less、router,终于实现了一个简单的登录表单。
代码如下:
import React from 'react';
import { Input, Button, message } from "antd";
import { UserOutlined, LockOutlined, EyeInvisibleOutlined, EyeTwoTone } from '@ant-design/icons';
import './index.less'
class Login extends React.Component{
constructor(props) {
super(props)
this.state = {
username: '',
password: ''
}
};
submit=()=>{
console.log(this.state)
if (this.state.username !== '' && this.state.password !== '') {
this.props.history.push('/Index')
} else {
message.error("用户名和密码不能为空")
}
};
user_change=(e)=>{
this.setState({
username: e.target.value
})
}
password_change=(e)=>{
this.setState({
password: e.target.value
})
}
render () {
const {username, password} = this.state
return (
<div className="login">
<Input
value={username}
onChange={this.user_change}
size="large"
placeholder="用户名"
prefix={<UserOutlined />} />
<Input.Password
value={password}
onChange={this.password_change}
size="large"
className="login__input"
placeholder="密码"
prefix={<LockOutlined />}
iconRender={visible => (visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />)}
/>
<Button
className="login__btn"
size="large"
type="primary"
onClick={this.submit}
>
登录
</Button>
</div>
);
}
}
export default Login;
推荐阅读
-
react组件从搭建脚手架到在npm发布的步骤实现
-
JavaScript在form表单中使用button按钮实现submit提交方法
-
Express+Nodejs 下的登录拦截实现代码
-
React利用插件和不用插件实现双向绑定的方法详解
-
express+vue+mongodb+session 实现注册登录功能
-
如何安装绿色版MySQL Community Server 5.7.16并实现远程登录
-
PHP实现QQ登录实例代码
-
JQuery+Ajax+Struts2+Hibernate框架整合实现完整的登录注册
-
react实现换肤功能的示例代码
-
Bash 脚本实现每次登录到 Shell 时可以查看 Linux 系统信息