Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/demo/first-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: First Render
nav:
title: Demo
path: /demo
---

<code src="../examples/first-render.tsx"></code>
49 changes: 49 additions & 0 deletions docs/examples/first-render.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import './basic.less';
import Button from './components/Button';

const Demo = () => {
const renderStart = React.useRef(Date.now());
const [renderTime, setRenderTime] = React.useState(0);

React.useEffect(() => {
setRenderTime(Date.now() - renderStart.current);
}, []);

return (
<>
<p>Render Time: {renderTime}ms</p>
{Array(10000)
.fill(1)
.map((_, key) => (
<div key={key}>
<Button>Default</Button>
<Button type="primary">Primary</Button>
<Button type="ghost">Ghost</Button>
<Button className="btn-override">Override By ClassName</Button>
</div>
))}
</>
);
};

export default function App() {
const [show, setShow] = React.useState(false);

return (
<div style={{ background: 'rgba(0,0,0,0.1)', padding: 16 }}>
<h3>默认情况下不会自动删除添加的样式</h3>

<label>
<input type="checkbox" checked={show} onChange={() => setShow(!show)} />
Show Components
</label>

{show && (
<div>
<Demo />
</div>
)}
</div>
);
}
Comment on lines +41 to +49
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

考虑添加更多上下文信息

当前示例缺乏与PR修改(缓存效果)的明确关联说明。建议添加解释文本,说明此示例如何展示缓存效果优化及其性能影响。

  {show && (
    <div>
+     <p>此示例展示了优化后的缓存效果如何提高大量样式组件的首次渲染性能。较低的渲染时间表明缓存效果改进有效。</p>
      <Demo />
    </div>
  )}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{show && (
<div>
<Demo />
</div>
)}
</div>
);
}
{show && (
<div>
<p>此示例展示了优化后的缓存效果如何提高大量样式组件的首次渲染性能。较低的渲染时间表明缓存效果改进有效。</p>
<Demo />
</div>
)}
🤖 Prompt for AI Agents (early access)
In docs/examples/first-render.tsx around lines 41 to 49, the example lacks clear
explanation linking it to the PR changes about caching effects. Add descriptive
text or comments in the component or nearby to explain how this example
demonstrates caching optimization and its impact on performance, providing
better context for readers.

13 changes: 12 additions & 1 deletion src/hooks/useGlobalCache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type ExtractStyle<CacheValue> = (
},
) => [order: number, styleId: string, style: string] | null;

const effectMap = new Map<string, boolean>();

export default function useGlobalCache<CacheType>(
prefix: string,
keyPath: KeyType[],
Expand Down Expand Up @@ -84,7 +86,15 @@ export default function useGlobalCache<CacheType>(
// which will clear cache on the first time.
buildCache(([times, cache]) => {
if (polyfill && times === 0) {
onCacheEffect?.(cacheContent);
if (!effectMap.has(fullPathStr)) {
onCacheEffect?.(cacheContent);
effectMap.set(fullPathStr, true);

// 微任务清理缓存,可以认为是单次 batch render 中只触发一次 effect
Promise.resolve().then(() => {
effectMap.delete(fullPathStr);
});
}
}
return [times + 1, cache];
});
Expand All @@ -97,6 +107,7 @@ export default function useGlobalCache<CacheType>(
if (nextCount === 0) {
// Always remove styles in useEffect callback
register(() => {
effectMap.delete(fullPathStr);
// With polyfill, registered callback will always be called synchronously
// But without polyfill, it will be called in effect clean up,
// And by that time this cache is cleaned up.
Expand Down
4 changes: 3 additions & 1 deletion tests/animation.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe('animation', () => {
});
});

it('re-mount should not missing animation style', () => {
it('re-mount should not missing animation style', async () => {
function genComp(cls: string) {
return () => {
const [token, hashId] = useCacheToken(theme, [baseToken], {
Expand Down Expand Up @@ -210,6 +210,8 @@ describe('animation', () => {
// Clean up
document.head.innerHTML = '';

await Promise.resolve();

// Render again
render(
<StyleProvider cache={createCache()}>
Expand Down