简体   繁体   English

仅重新渲染 JSX map function 中的特定子组件

[英]Rerender only specific Child Components in JSX map function

I am mapping through an array, which returns JSX Components for each of the items in the array.我正在通过一个数组进行映射,该数组为数组中的每个项目返回 JSX 组件。 During runtime I want to pass down values.在运行时我想传递值。 If they match the value of the individual items, their individual component gets modified.如果它们与单个项目的值匹配,则它们的单个组件将被修改。

I am trying to find a way to achieve this without rerendering all components, which currently happens because the props change我试图找到一种方法来实现这一点,而无需重新渲染所有组件,目前发生这种情况是因为道具发生了变化

I have tried using shouldComponentUpdate in a class component, but it seems this way I can only compare prevState and prevProps with the corresponding changes.我曾尝试在 class 组件中使用shouldComponentUpdate ,但似乎这样我只能将 prevState 和 prevProps 与相应的更改进行比较。 I have further considered useMemo in the Map function, which didnt work, because it was nested inside the map function.我还在Z46F3EA056CAAA3126B91F3F70BEEA0BEEA068CZ ZC1C1C425268E68385D174C174C174F14F中进一步考虑了USEMO,因为它不起作用,因为它嵌套在Z1D78DC8DC8112121214EED514EED514EED14EED14EENETED.14ED.114EED.514ED.114EED.14EED.14ED.14ED.14ED.14ED.14ED.14EED.14ED.14ED.14ED.14ED.14ED.14ED.14ED.14ED.14ED.114EDEF114ED.14ENETEDB.ERE

const toParent=[1,2,4,5]

Parent Component:父组件:

function parent({ toParent }) {

const [myNumbers] = useState([1,2,3,4, ..., 1000]);

return (
   <div>
      {myNumbers.map((number, index) => (
         <Child toChild = { toParent } number = { number } 
          index= { index } key = { number }/>
      ))}
   </div>
  )
}

Child Component:子组件:

function Child({toChild, number, index}){
   const [result, setResult] = useState(() => { return number*index }

   useEffect(()=> {
      if (toChild.includes(number)) {
         let offset = 10
         setResult((prev)=> { return { prev+offset }})
      }
   }, [toChild])

   return ( 
      <div style={{width: result}}> Generic Div </div> )
}

The solution to my problem was using the React.memo HOC and comparing the properties to one another and exporting it as React.memo(Child, propsAreEqual) .我的问题的解决方案是使用 React.memo HOC并将属性相互比较并将其导出为React.memo(Child, propsAreEqual)

Performance表现

This way other methods like findElementbyId (not recommended in any case) and shouldComponentUpdate to target specific items in a map function can be avoided.这样可以避免使用 findElementbyId(在任何情况下都不推荐)和 shouldComponentUpdate 等其他方法来定位 map function 中的特定项目。 Performance is quite good, too.性能也相当不错。 Using this method cut down the rendering time from 40ms every 250ms to about 2 ms.使用这种方法将渲染时间从每 250 毫秒 40 毫秒减少到大约 2 毫秒。

Implementation执行

In Child Component:在子组件中:

function Child(){...}
function propsAreEqual(prev, next) {
   //returning false will update component, note here that nextKey.number never changes.
   //It is only constantly passed by props
    return !next.toChild.includes(next.number)

}
export default React.memo(Child, propsAreEqual);

or alternatively, if other statements should be checked as well:或者,如果还应检查其他语句:

function Child(){...}
function propsAreEqual(prev, next) {

   if (next.toChild.includes(next.number)) { return false }
   else if ( next.anotherProperty === next.someStaticProperty ) { return false }
   else { return true }
  }

export default React.memo(Key, propsAreEqual);

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

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