網站首頁 編程語言 正文
Form 樣式
首先來看一個簡單Form, 式樣如下
import * as React from 'react';
const LoginForm = () => {
return (
<form>
<div>
// Notice: 這里要用htmlFor,相當于id
<label htmlFor="email">Email</label>
<input id="email" type="text" />
</div>
<div>
<label htmlFor="password">Password</label>
<input id="password" type="password" />
</div>
<button>Submit</button>
</form>
);
};
export { LoginForm };
屏幕顯示如下
若給以上Form加入用戶輸入, 可引入handleSubmit指令,并設置onSubmit事件觸發,具體如下
(關于用戶輸入View => App單向數據流,可參考相關文章 - 學習React的特征(一) - 單向數據流?
import * as React from 'react';
const LoginForm = () => {
// handleSubmit here
const handleSubmit = (e) => {
// preventDefault用于防止部分瀏覽器一些默認的不必要的行為
event.preventDefault();
};
return (
// onSubmit here
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<input id="email" type="text" />
</div>
<div>
<label htmlFor="password">Password</label>
<input id="password" type="password" />
</div>
<button type="submit">Submit</button>
</form>
);
};
export { LoginForm };
接著,進一步獲取用戶的輸入, 可通過e.target.elements來抓取
import * as React from 'react';
const LoginForm = () => {
const handleSubmit = (e) => {
e.preventDefault();
// 獲取input elements
const email = e.target.elements.email.value;
const password = e.target.elements.password.value;
alert(email + ' ' + password);
};
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<input id="email" type="text" />
</div>
<div>
<label htmlFor="password">Password</label>
<input id="password" type="password" />
</div>
<button type="submit">Submit</button>
</form>
);
};
export { LoginForm };
屏幕顯示如下
點擊Submit, 顯示如下
React hook
或許用React hook更簡潔優雅些 , 引入useState管理email, password狀態
import * as React from 'react';
const LoginForm = () => {
// Add hooks here
const [email, setEmail] = React.useState('');
const [password, setPassword] = React.useState('');
const handleEmail = (e) => {
setEmail(e.target.value);
};
const handlePassword = (e) => {
setPassword(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
alert(email + ' ' + password);
};
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<input
id="email"
type="text"
value={email}
onChange={handleEmail}
/>
</div>
<div>
<label htmlFor="password">Password</label>
<input
id="password"
type="password"
value={password}
onChange={handlePassword}
/>
</div>
<button type="submit">Submit</button>
</form>
);
};
export { LoginForm };
到這里一個React Form雛形基本完成,當然Form遇到的問題遠不止這些, 后續會再進一步探討Form數據管理,驗證等方案
原文鏈接:https://juejin.cn/post/7143478192827793445
相關推薦
- 2022-12-25 Redis中AOF與RDB持久化策略深入分析_Redis
- 2022-01-31 pytorch:tensor與numpy轉換 & .cpu.numpy()和.numpy()
- 2022-09-26 淺談Redis如何應對并發訪問_Redis
- 2022-06-12 C#異步編程的三種模式_C#教程
- 2022-12-30 React高階組件使用教程詳解_React
- 2022-06-11 C#把DataTable導出為Excel文件_C#教程
- 2022-11-21 Qt實現小功能之圓形進度條的方法詳解_C 語言
- 2022-07-16 springclud 服務與服務之間調用(提供者 接口 消費者)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支