React特征學(xué)習(xí)之Form格式示例詳解
Form 樣式
首先來看一個簡單Form, 式樣如下
import * as React from 'react';
const LoginForm = () => {
return (
<form>
<div>
// Notice: 這里要用htmlFor,相當(dāng)于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指令,并設(shè)置onSubmit事件觸發(fā),具體如下
(關(guān)于用戶輸入View => App單向數(shù)據(jù)流,可參考相關(guān)文章 - 學(xué)習(xí)React的特征(一) - 單向數(shù)據(jù)流
import * as React from 'react';
const LoginForm = () => {
// handleSubmit here
const handleSubmit = (e) => {
// preventDefault用于防止部分瀏覽器一些默認(rèn)的不必要的行為
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 };
接著,進(jìn)一步獲取用戶的輸入, 可通過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更簡潔優(yōu)雅些 , 引入useState管理email, password狀態(tài)
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雛形基本完成,當(dāng)然Form遇到的問題遠(yuǎn)不止這些, 后續(xù)會再進(jìn)一步探討Form數(shù)據(jù)管理,驗證等方案
以上就是React特征學(xué)習(xí)之Form格式示例詳解的詳細(xì)內(nèi)容,更多關(guān)于React特征Form格式的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
2023年最新react面試題總結(jié)大全(附詳細(xì)答案)
React是一種廣泛使用的JavaScript庫,為構(gòu)建用戶界面提供了強大的工具和技術(shù),這篇文章主要給大家介紹了關(guān)于2023年最新react面試題的相關(guān)資料,文中還附有詳細(xì)答案,需要的朋友可以參考下2023-10-10
React?Hooks useReducer?逃避deps組件渲染次數(shù)增加陷阱
這篇文章主要介紹了React?Hooks?之?useReducer?逃避deps后增加組件渲染次數(shù)的陷阱詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
React中使用async validator進(jìn)行表單驗證的實例代碼
react上進(jìn)行表單驗證是很繁瑣的,在這里使用async-validator處理起來就變的很方便了,接下來通過本文給大家介紹React中使用async validator進(jìn)行表單驗證的方法,需要的朋友可以參考下2018-08-08
字節(jié)封裝React組件手機號自動校驗格式FormItem
這篇文章主要為大家介紹了字節(jié)封裝React組件手機號自動校驗格式FormItem,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

