React修改數(shù)組對象的注意事項(xiàng)及說明
React修改數(shù)組對象問題
react開發(fā)主張使用函數(shù)式編程,函數(shù)式編程有個重要的特性就是不可變性。
你無法更改數(shù)據(jù),也不能更改。 如果要改變或更改數(shù)據(jù),則必須復(fù)制數(shù)據(jù)副本來更改。
看個例子,就是Vue和React兩個框架實(shí)現(xiàn)給數(shù)組添加一個元素。
vue
export default { ?? ?name: "home", ?? ?data() { ?? ??? ?return { ?? ??? ??? ?testArr: ['蘋果','香蕉'] ?? ??? ?}; ? ? }, ? ? created(){ ? ? ? ? this.testArr.push('橘子') ? ? } }; ...
react
class App extends React.Component { ? ? constructor(props) { ? ? ? ? super(props); ? ? ? ? this.state = { ? ? ? ? ? ? testArr: ['蘋果','香蕉'] ? ? ? ? }; ? ? } ? ? componentDidMount(){ ? ? ? ? this.setState({ ? ? ? ? ? ? testArr:[...this.state.testArr,'橘子'] ? ? ? ? }) ? ? } ? ? render(){ ? ? ? ? return ( ? ? ? ? ? ? <React.Fragment> ? ? ? ? ? ? ? ? <p>{this.state.testArr}</p> ? ? ? ? ? ? </React.Fragment> ? ? ? ? ) ? ? } }
這里會發(fā)現(xiàn)兩個框架有個細(xì)微差別,Vue是直接修改的原數(shù)組,而React是不修改原數(shù)組,而是創(chuàng)建了一份新的數(shù)組,再通過setState賦值。剛接觸React的時候的確會覺得挺奇怪,感覺會無形增加代碼復(fù)雜度。接下來看下為何React要如此設(shè)計(jì)。
React遵循函數(shù)式編程規(guī)范。在函數(shù)式編程中,不推薦直接修改原始數(shù)據(jù)。 如果要改變或更改數(shù)據(jù),則必須復(fù)制數(shù)據(jù)副本來更改。所以,函數(shù)式編程中總是生成原始數(shù)據(jù)的轉(zhuǎn)換副本,而不是直接更改原始數(shù)據(jù)。
這里是一些常見的React修改數(shù)組或者對象的例子,所有這些函數(shù)都不改變現(xiàn)有的數(shù)據(jù),而是返回新的數(shù)組或?qū)ο蟆?/p>
刪除數(shù)組中的指定元素
//刪除testArr中的櫻桃 ... constructor(props) { ? ? super(props); ? ? this.state = { ? ? ? ? testArr: ['蘋果','香蕉','橘子','櫻桃','橙子'] ? ? }; } componentDidMount(){ ? ? this.setState({ ? ? ? ? testArr:this.state.testArr.filter(res=>res!=='櫻桃') ? ? }) } ...
合并兩個對象
... constructor(props) { ? ? super(props); ? ? this.state = { ? ? ? ? testObj1:{ ? ? ? ? ? ? chineseName:'橘子', ? ? ? ? ? ? englishName:'orange' ? ? ? ? }, ? ? ? ? testObj2:{ ? ? ? ? ? ? color:'yellow', ? ? ? ? ? ? shape:'circle' ? ? ? ? }, ? ? ? ? testObj:{} ? ? }; } componentDidMount() { ? ? this.setState({ ? ? ? ? testObj: Object.assign(this.state.testObj1,this.state.testObj2) ? ? }) } ...
修改多層級對象的值
//testObj的apple的color改成green ... constructor(props) { ? ? super(props); ? ? this.state = { ? ? ? ? testObj: { ? ? ? ? ? ? banner: { ? ? ? ? ? ? ? ? name: '香蕉', ? ? ? ? ? ? ? ? color: 'yellow' ? ? ? ? ? ? }, ? ? ? ? ? ? apple: { ? ? ? ? ? ? ? ? name: '蘋果', ? ? ? ? ? ? ? ? color: 'red' ? ? ? ? ? ? } ? ? ? ? } ? ? }; } componentDidMount() { ? ? this.setState({ ? ? ? ? testObj: { ? ? ? ? ? ? ...this.state.testObj, ? ? ? ? ? ? apple:{ ? ? ? ? ? ? ? ? ...this.state.testObj.apple, ? ? ? ? ? ? ? ? color:'green' ? ? ? ? ? ? } ? ? ? ? } ? ? }) } ...
React修改數(shù)組中某個參數(shù)值方法
const currentPageVideoList=[...this.state.currentPageVideoList]; this.setState({currentPageVideoList: currentPageVideoList.map((item, key)=>? ? ? key==index?{...item, coverImg: this.state.defaultImg}:item )})
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
VSCode配置react開發(fā)環(huán)境的步驟
本篇文章主要介紹了VSCode配置react開發(fā)環(huán)境的步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12React中useCallback useMemo使用方法快速精通
在React函數(shù)組件中,當(dāng)組件中的props發(fā)生變化時,默認(rèn)情況下整個組件都會重新渲染。換句話說,如果組件中的任何值更新,整個組件將重新渲染,包括沒有更改values/props的函數(shù)/組件。在react中,我們可以通過memo,useMemo以及useCallback來防止子組件的rerender2023-02-02React類組件中super()和super(props)的區(qū)別詳解
這篇文章給大家詳細(xì)介紹了React類組件中super()和super(props)有什么區(qū)別,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01Shopee在React?Native?架構(gòu)方面的探索及發(fā)展歷程
這篇文章主要介紹了Shopee在React?Native?架構(gòu)方面的探索,本文會從發(fā)展歷史、架構(gòu)模型、系統(tǒng)設(shè)計(jì)、遷移方案四個方向逐一介紹我們?nèi)绾我徊讲降貪M足多團(tuán)隊(duì)在復(fù)雜業(yè)務(wù)中的開發(fā)需求,需要的朋友可以參考下2022-07-07React中useEffect 與 useLayoutEffect的區(qū)別
本文主要介紹了React中useEffect與useLayoutEffect的區(qū)別,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07antd中form表單的wrapperCol和labelCol問題詳解
最近學(xué)習(xí)中遇到了些問題,所以給大家總結(jié),下面這篇文章主要給大家介紹了關(guān)于antd中form表單的wrapperCol和labelCol問題的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02