React特征Form?單向數(shù)據(jù)流示例詳解
引言
今天將之前的內(nèi)容做個(gè)系統(tǒng)整理,結(jié)合 React Form 案例, 來了解下為何React推薦單向數(shù)據(jù)流,如果采用雙向管理,可能的問題 (關(guān)于React Form案例,可參考相關(guān)文章 - 學(xué)習(xí)React的特征(二) - React Form
集中狀態(tài)管理
首先來看之前的React Form, 若采用單向數(shù)據(jù)流
import * as React from 'react';
const Useremail = props => <input type="email" {...props} />
const Userpassword = props => <input type="password" {...props} />
const SubmitButton = props => <button type="submit" {...props} />
const LoginForm = () => {
// LoginForm狀態(tài)變化 => Useremail/Userpassword組件
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>
<Useremail
id="email"
type="text"
value={email}
onChange={handleEmail}
/>
</div>
<div>
<label htmlFor="password">Password</label>
<Userpassword
id="password"
type="password"
value={password}
onChange={handlePassword}
/>
</div>
<SubmitButton type="submit">Submit</SubmitButton>
</form>
);
};
export { LoginForm };
可以看到, 每次Useremail or Password 輸入發(fā)生改變時(shí),LoginForm中的email與password狀態(tài)動(dòng)態(tài)產(chǎn)生改變
雙向數(shù)據(jù)流
import * as React from 'react';
// Useremail/Userpassword組件狀態(tài)變化 => LoginForm父組件
const Useremail = ({updateUseremail, ...props}) =>
<input type="email"
onChange={e => updateUseremail(e.target.value)}
{...props} />
const Userpassword = ({updateUserpassword, ...props}) =>
<input type="password"
onChange={e => updateUserpassword(e.target.value)}
{...props} />
const SubmitButton = props => <button type="submit" {...props} />
const LoginForm = () => {
// LoginForm狀態(tài)變化 => Useremail/Userpassword組件
const [email, setEmail] = React.useState('');
const [password, setPassword] = React.useState('');
const handleSubmit = (e) => {
e.preventDefault();
alert(email + ' ' + password);
};
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<Useremail
id="email"
type="text"
value={email}
updateUseremail={setEmail}
/>
</div>
<div>
<label htmlFor="password">Password</label>
<Userpassword
id="password"
type="password"
value={password}
updateUserpassword={setPassword}
/>
</div>
<SubmitButton type="submit">Submit</SubmitButton>
</form>
);
};
export { LoginForm };
實(shí)際執(zhí)行這兩個(gè)程序,得到的結(jié)果是一致,看起來兩者沒有什么區(qū)別
那為何不選擇雙向數(shù)據(jù)流
- 代碼維護(hù)
以上代碼可以發(fā)現(xiàn),一旦LoginForm發(fā)生問題,雙向數(shù)據(jù)流需要在多個(gè)子組件和父組件中同時(shí)尋找狀態(tài)異常的原因,當(dāng)程序逐漸趨于復(fù)雜化,后期維護(hù)會(huì)變得異常困難
- 組件復(fù)用
每次用戶狀態(tài)需求發(fā)生新的變化,每個(gè)子組件都要相應(yīng)調(diào)整,造成組件在實(shí)際使用中難以復(fù)用
- 應(yīng)用升級(jí)
另外,當(dāng)程序需要做整體升級(jí),因?yàn)闋顟B(tài)分散在各個(gè)組件,將會(huì)導(dǎo)致難以實(shí)行
小結(jié)
組件設(shè)計(jì)成響應(yīng)式,從長(zhǎng)遠(yuǎn)看,更符合React推薦的設(shè)計(jì)模式

以上就是React特征Form 單向數(shù)據(jù)流示例詳解的詳細(xì)內(nèi)容,更多關(guān)于React特征Form 單向數(shù)據(jù)流的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
React跨端動(dòng)態(tài)化之從JS引擎到RN落地詳解
這篇文章主要為大家介紹了React跨端動(dòng)態(tài)化之從JS引擎到RN落地,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
React 數(shù)據(jù)獲取與性能優(yōu)化詳解
這篇文章主要為大家介紹了React 數(shù)據(jù)獲取與性能優(yōu)化方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
在react中對(duì)less實(shí)現(xiàn)scoped配置方式
這篇文章主要介紹了在react中對(duì)less實(shí)現(xiàn)scoped配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
使用React?SSR寫Demo一學(xué)就會(huì)
這篇文章主要為大家介紹了使用React?SSR寫Demo實(shí)現(xiàn)教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
react中實(shí)現(xiàn)搜索結(jié)果中關(guān)鍵詞高亮顯示
這篇文章主要介紹了react中實(shí)現(xiàn)搜索結(jié)果中關(guān)鍵詞高亮顯示,使用react實(shí)現(xiàn)要比js簡(jiǎn)單很多,方法都是大同小異,具體實(shí)現(xiàn)代碼大家跟隨腳本之家小編一起看看吧2018-07-07
React Native react-navigation 導(dǎo)航使用詳解
本篇文章主要介紹了React Native react-navigation 導(dǎo)航使用詳解,詳解的介紹了react-navigation導(dǎo)航的使用,具有一定的參考價(jià)值,有興趣的可以了解一下2017-12-12
淺談React的React.FC與React.Component的使用
本文主要介紹了React的React.FC與React.Component的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
詳解如何用webpack4從零開始構(gòu)建react開發(fā)環(huán)境
這篇文章主要介紹了詳解如何用webpack4從零開始構(gòu)建react開發(fā)環(huán)境,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01

