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)文章
React-Native實現(xiàn)ListView組件之上拉刷新實例(iOS和Android通用)
本篇文章主要介紹了React-Native實現(xiàn)ListView組件之上拉刷新實例(iOS和Android通用),具有一定的參考價值,有興趣的可以了解一下2017-07-07
next-redux-wrapper使用細(xì)節(jié)及源碼分析
這篇文章主要為大家介紹了next-redux-wrapper使用細(xì)節(jié)及源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
React應(yīng)用中避免白屏現(xiàn)象的方法小結(jié)
在開發(fā)React應(yīng)用程序時,我們都曾遇到過這樣的場景:一個未被捕獲的異常突然中斷了組件的渲染流程,導(dǎo)致用戶界面呈現(xiàn)出一片空白,也就是俗稱的“白屏”現(xiàn)象,本文將探討如何在React應(yīng)用中有效捕獲并處理這些錯誤,避免白屏現(xiàn)象的發(fā)生,需要的朋友可以參考下2024-06-06
react-three-fiber實現(xiàn)炫酷3D粒子效果首頁
這篇文章主要介紹了react-three-fiber實現(xiàn)3D粒子效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
Redux DevTools不能顯示數(shù)據(jù)問題
這篇文章主要介紹了Redux DevTools不能顯示數(shù)據(jù)問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
在Ant Design Pro登錄功能中集成圖形驗證碼組件的方法步驟
這篇文章主要介紹了在Ant Design Pro登錄功能中集成圖形驗證碼組件的方法步驟,這里的登錄功能其實就是一個表單提交,實現(xiàn)起來也很簡單,具體實例代碼跟隨小編一起看看吧2021-05-05

