简体   繁体   English

更新数组中的1个元素是否会在React中为数组中的其他元素触发渲染?

[英]Does updating 1 element in an array trigger a render in React for other elements in array?

I am using React with Redux for my current project. 我正在使用React和Redux来完成我当前的项目。

I have a state object like so: 我有一个像这样的状态对象:

state: {
    reducerOne: {
         keyOne: valueOne,
         keyTwo: valueTwo
    }
    reducerTwo: {
         keyThree: valueThree
         keyFour: valueFour
    }
}

Suppose valueFour is an array of objects. 假设valueFour是一个对象数组。 I use the valueFour array to map a bunch of React elements like so in the render method: 我使用valueFour数组在render方法中map一堆React元素:

this.props.valueFour.map(()=> <div/>)

(Obviously the above is simplified but all that I am trying to indicate is that I am rendering a bunch of elements) (显然以上是简化的,但我想表明的是我渲染了一堆元素)

Now, I would like to update only 1 of the above elements. 现在,我想只更新上述元素中的一个。 In my action/reducer code I return the entire valueFour array with the 1 element updated in a function like so: 在我的action / reducer代码中,我返回整个valueFour数组,在这样的函数中更新了1个元素:

action code 行动代码

export function updateThingInArray(elementId){
    return (dispatch, getState) => {
        const myArray = getState().reducerTwo.keyFour
        for (let i=0; i < myArray.length; i++) {
           if (myArray[i].id === elementId){
               const result= [
                    ...myArray.slice(0,i),
                    Object.assign({},
                       myArray[i],
                       {field:newValue}),
                    ...myArray.slice(i+1)
               ]
               dispatch(update(result))
           }
        }
     }
}

reducer code: 减速机代码:

export default handleActions({
  [UPDATE]: (state,{payload}) => Object.assign({},state, {keyFour: payload})
},{
  keyThree:{},
  keyFour:{}
})

(My reducer code uses the library redux-actions) (我的reducer代码使用库redux-actions)

I hope all this makes sense. 我希望这一切都有道理。 If it doesn't please ask and I will clarify. 如果没有请问,我会澄清。

But my question is, since keyFour has the entireArray technically updated, does that mean ALL my elements will update even though only 1 has new data? 但我的问题是,因为keyFour在整个技术上更新了整个阵列,这是否意味着所有我的元素都会更新,即使只有1个有新数据? If so, is there a more efficient way of implementing what I am currently doing? 如果是这样,有没有更有效的方式来实现我目前正在做的事情?

The render method will be called again, which means it will map through that array again. 将再次调用render方法,这意味着它将再次映射该数组。 You can look at the elements tab in chrome and you can see the html that flashes, it is the part that changed. 您可以查看chrome中的元素选项卡,您可以看到闪烁的html,它是更改的部分。 If the component has the same data and the same key, the actual html won't be changed, but the render method will be called in the children. 如果组件具有相同的数据和相同的键,则不会更改实际的html,但会在子项中调用render方法。

Edit: didn't realize you were updating a nested key, React only checks shallow equality: https://facebook.github.io/react/docs/shallow-compare.html 编辑:没有意识到你正在更新嵌套键,React只检查浅平等: https//facebook.github.io/react/docs/shallow-compare.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM