welldone-software / why-did-you-render

why-did-you-render by Welldone Software monkey patches React to notify you about potentially avoidable re-renders. (Works with React Native as well.)
https://www.npmjs.com/package/@welldone-software/why-did-you-render
MIT License
11.2k stars 201 forks source link

use React.lazy warp function, wdyr not work. #268

Open m5ultra opened 1 year ago

m5ultra commented 1 year ago
image
import { Button } from 'antd'
import { useState, lazy } from 'react'
// import BigListPureComponent from './32.1.BigListPureComponent' // wdyr work
const BigListPureComponent = lazy(
  // wdyr work
  async () => await import('./32.1.BigListPureComponent')
)

export const Test = () => {
  const [count, setCount] = useState(0)

  /* use the hook instead of the const to prevent
   ** "BigListPureComponent" from re-rendering wit this component */

  // const bigListStyle = React.useMemo(() => ({ width: "100%" }), [])

  const bigListStyle = {
    width: '100%',
  }

  return (
    <div className="Main">
      <h1>Big List (Main Demo)</h1>
      <p>
        Open the console and notice how the heavy list re-renders on every click
        on Increase! even though its props are the same.
      </p>
      <div>
        <Button
          onClick={() => {
            setCount((c) => c + 1)
          }}
        >
          Increase! Count: {count}
        </Button>
      </div>
      <BigListPureComponent style={bigListStyle} />
    </div>
  )
}
import { memo } from 'react'
import { times } from 'lodash'
const BigListPureComponent = memo(({ style }: { style: Object }) => {
  console.log(
    "BigListPureComponent Re-Render! - We don't want this to happen too often."
  )
  return (
    <div style={style}>
      <h2>BigListPureComponent</h2>
      <div>
        {times(1000).map((n) => (
          <div key={n}>Element #{n}</div>
        ))}
      </div>
    </div>
  )
})

BigListPureComponent.displayName = 'BigListPureComponent'

export default BigListPureComponent