Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP ✨pref: improve memorization #175

Open
wants to merge 3 commits into
base: alpha
Choose a base branch
from

Conversation

Wxh16144
Copy link
Member

我不确定有多少提升,最近在抓内存飙升问题, 看了一遍 antd 的 cssinjs 包括 antd-style

如下代码:

先不管乱不乱,case 是 createStyles 创建的 useFooStyles 这个 hook 可以接受一个参数是 props。

antd-style 侧虽然用了 usememo,但是每次 re-render 的时候 props 是一个新的对象,导致 useMemo 其实没有效果。

const useFooStyles = createStyles(({ css, cx, token }, { show }: { show: boolean }) => ({
  wrap: cx(
    css({
      display: 'inline-block',
      height: 100,
      position: 'relative',
      border: `1px solid ${token.colorPrimary}`,
    },
      show ?
        css({
          '&::before': {
            content: '""',
            position: 'absolute',
            insetBlockStart: 0,
            insetInlineEnd: 0,
            width: 20,
            height: 20,
            backgroundColor: token.colorPrimary,
            transform: 'translate(100%, -100%)',
          }
        })
        : null
    )
  )
}));

const Foo = ({ count = 0 }: FooProps) => {
  console.log("Foo-render");
  const { styles } = useFooStyles({
    show: count % 3 === 0
  });

  return <div className={styles.wrap}>Foo-{count ?? 'unknown'}</div>;
}

这个 PR 改为用 rc-util 提供的 usememo ,他可以自定义对比上一次和当前的依赖逻辑, 我这里使用浅比较。

@arvinxx arvinxx changed the base branch from master to alpha October 23, 2024 12:20
Copy link
Collaborator

@arvinxx arvinxx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改完帮你发个 alpha 测下

src/factories/createStyles/index.ts Outdated Show resolved Hide resolved

return useMemo(() => {
return React.useMemo(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不要加 React. 。 antd-style 最小依赖版本号是 18,不应该加

@arvinxx
Copy link
Collaborator

arvinxx commented Oct 23, 2024

另外有空可以看看 #173#158

@Wxh16144 Wxh16144 changed the title ✨pref: improve memorization WIP ✨pref: improve memorization Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants