React修改數(shù)組對象的注意事項及說明
React修改數(shù)組對象問題
react開發(fā)主張使用函數(shù)式編程,函數(shù)式編程有個重要的特性就是不可變性。
你無法更改數(shù)據(jù),也不能更改。 如果要改變或更改數(shù)據(jù),則必須復制數(shù)據(jù)副本來更改。
看個例子,就是Vue和React兩個框架實現(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)兩個框架有個細微差別,Vue是直接修改的原數(shù)組,而React是不修改原數(shù)組,而是創(chuàng)建了一份新的數(shù)組,再通過setState賦值。剛接觸React的時候的確會覺得挺奇怪,感覺會無形增加代碼復雜度。接下來看下為何React要如此設(shè)計。
React遵循函數(shù)式編程規(guī)范。在函數(shù)式編程中,不推薦直接修改原始數(shù)據(jù)。 如果要改變或更改數(shù)據(jù),則必須復制數(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用react-activation實現(xiàn)keepAlive支持返回傳參
本文主要介紹了使用react-activation實現(xiàn)keepAlive支持返回傳參,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05
在Ant Design Pro登錄功能中集成圖形驗證碼組件的方法步驟
這篇文章主要介紹了在Ant Design Pro登錄功能中集成圖形驗證碼組件的方法步驟,這里的登錄功能其實就是一個表單提交,實現(xiàn)起來也很簡單,具體實例代碼跟隨小編一起看看吧2021-05-05
React Router中Link和NavLink的學習心得總結(jié)
這篇文章主要介紹了React Router中Link和NavLink的學習心得總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
解決React報錯React.Children.only expected to rece
這篇文章主要為大家介紹了React報錯React.Children.only expected to receive single React element child分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
基于PixiJS實現(xiàn)react圖標旋轉(zhuǎn)動效
PixiJS是一個開源的基于web的渲染系統(tǒng),為游戲、數(shù)據(jù)可視化和其他圖形密集型項目提供了極快的性能,這篇文章主要介紹了用PixiJS實現(xiàn)react圖標旋轉(zhuǎn)動效,需要的朋友可以參考下2022-05-05

