-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b32e1c7
commit 426100e
Showing
5 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
sidebar_position: 5 | ||
--- | ||
|
||
import MemoDirectiveExample from '@site/src/examples/memo'; | ||
|
||
# Memo Directive | ||
|
||
This directive memoizes a component with `React.memo` to avoid unnecessary rerenders. | ||
|
||
```jsx | ||
const memoMap = new Map(); | ||
|
||
function memoMiddleware(next, type, { $memo, ...props }, key) { | ||
if ($memo && typeof type === 'function') { | ||
let memoed = memoMap.get(type); | ||
if (!memoed) { | ||
memoed = React.memo(type); | ||
memoMap.set(type, memoed); | ||
} | ||
|
||
return next(memoed, props, key); | ||
} | ||
|
||
return next(type, props, key); | ||
} | ||
|
||
addMiddlewares(memoMiddleware); | ||
``` | ||
|
||
## Usage | ||
|
||
```jsx | ||
function App() { | ||
// ... | ||
return ( | ||
<div> | ||
<Memoed>This is not memoed.</Memoed> | ||
<Memoed $memo>This is memoed.</Memoed> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
<MemoDirectiveExample /> | ||
|
||
## Typescript types | ||
|
||
```ts | ||
declare module 'react' { | ||
interface Attributes { | ||
$memo?: boolean; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 1 | ||
sidebar_position: 6 | ||
--- | ||
|
||
import Example from '@site/src/examples/onwheel-active'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** @jsxImportSource . */ | ||
|
||
import { PropsWithChildren, useEffect, useRef, useState } from 'react'; | ||
import BrowserWindow from '../../components/BrowserWindow'; | ||
|
||
function Memoed({ children }: PropsWithChildren) { | ||
const a = useRef(0); | ||
|
||
return ( | ||
<p> | ||
<span>{children}</span> | ||
<span> Render count: {++a.current}</span> | ||
</p> | ||
); | ||
} | ||
|
||
function App() { | ||
const [count, setCount] = useState(0); | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setCount((count) => count + 1); | ||
}, 1000); | ||
|
||
return () => { | ||
clearInterval(interval); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<p>Timer: {count}</p> | ||
|
||
<Memoed>This is not memoed.</Memoed> | ||
<Memoed $memo>This is memoed.</Memoed> | ||
|
||
</div> | ||
); | ||
} | ||
|
||
export default function IfDirectiveExample() { | ||
return ( | ||
<BrowserWindow> | ||
<App /> | ||
</BrowserWindow> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { memo } from 'react'; | ||
import { createLocalJsxContext } from '../setup'; | ||
|
||
const ctx = createLocalJsxContext(); | ||
export const { jsx, jsxDEV, jsxs } = ctx; | ||
|
||
|
||
const memoMap = new Map(); | ||
|
||
ctx.addMiddlewares(function memoMiddleware(next, type, { $memo, ...props }, key) { | ||
if ($memo && typeof type === 'function') { | ||
let memoed = memoMap.get(type); | ||
if (!memoed) { | ||
memoed = memo(type); | ||
memoMap.set(type, memoed); | ||
} | ||
|
||
return next(memoed, props, key); | ||
} | ||
|
||
return next(type, props, key); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters