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ù)會變得異常困難
- 組件復(fù)用
每次用戶狀態(tài)需求發(fā)生新的變化,每個(gè)子組件都要相應(yīng)調(diào)整,造成組件在實(shí)際使用中難以復(fù)用
- 應(yīng)用升級
另外,當(dāng)程序需要做整體升級,因?yàn)闋顟B(tài)分散在各個(gè)組件,將會導(dǎo)致難以實(shí)行
小結(jié)
組件設(shè)計(jì)成響應(yīng)式,從長遠(yuǎn)看,更符合React推薦的設(shè)計(jì)模式

以上就是React特征Form 單向數(shù)據(jù)流示例詳解的詳細(xì)內(nèi)容,更多關(guān)于React特征Form 單向數(shù)據(jù)流的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
React實(shí)現(xiàn)一個(gè)拖拽排序組件的示例代碼
這篇文章主要給大家介紹了React實(shí)現(xiàn)一個(gè)拖拽排序組件?-?支持多行多列、支持TypeScript、支持Flip動(dòng)畫、可自定義拖拽區(qū)域,文章通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
React中useState原理的代碼簡單實(shí)現(xiàn)
要實(shí)現(xiàn)useState的背后原理,則需要深入了解狀態(tài)是如何在函數(shù)組件的渲染周期中保持和更新的,本文將通過一段代碼簡單闡述useState鉤子函數(shù)的實(shí)現(xiàn)思路,希望對大家有所幫助2023-12-12
使用React+SpringBoot開發(fā)一個(gè)協(xié)同編輯的表格文檔實(shí)現(xiàn)步驟
隨著云計(jì)算和團(tuán)隊(duì)協(xié)作的興起,協(xié)同編輯成為了許多企業(yè)和組織中必不可少的需求,本文小編就將為大家介紹如何使用React+SpringBoot簡單的開發(fā)一個(gè)協(xié)同編輯的表格文檔,感興趣的朋友一起看看吧2023-11-11
react中的forwardRef 和memo的區(qū)別解析
forwardRef和memo是React中用于性能優(yōu)化和組件復(fù)用的兩個(gè)高階函數(shù),本文給大家介紹react中的forwardRef 和memo的區(qū)別及適用場景,感興趣的朋友跟隨小編一起看看吧2023-10-10
react-native 封裝視頻播放器react-native-video的使用
本文主要介紹了react-native 封裝視頻播放器react-native-video的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
react中代碼塊輸出,代碼高亮顯示,帶行號,能復(fù)制的問題
這篇文章主要介紹了react中代碼塊輸出,代碼高亮顯示,帶行號,能復(fù)制的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
React生命周期方法之componentDidMount的使用
這篇文章主要介紹了React生命周期方法之componentDidMount的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

