Skip to content

Commit 7f61914

Browse files
committed
more manual memo
1 parent f0b225b commit 7f61914

2 files changed

Lines changed: 46 additions & 24 deletions

File tree

demo/demo.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import {type PortableTextBlock, type PortableTextComponents, PortableText} from '@portabletext/react-latest'
1+
import {
2+
type PortableTextBlock,
3+
type PortableTextComponents,
4+
PortableText,
5+
} from '@portabletext/react'
6+
import {createClient} from '@sanity/client'
27
import {studioTheme, ThemeProvider} from '@sanity/ui'
38
import {StrictMode, Suspense, use, useEffect, useState} from 'react'
49
import {createRoot} from 'react-dom/client'
5-
import {createClient} from '@sanity/client'
610

711
import {AnnotatedMap} from './components/AnnotatedMap'
812
import {CharacterReference} from './components/CharacterReference'
@@ -49,8 +53,17 @@ const ptComponents: PortableTextComponents = {
4953
},
5054
}
5155

52-
const client = createClient({projectId: 'ppsg7ml5', dataset: 'test', apiVersion: '2025-12-09', useCdn: true, perspective: 'published'})
53-
const promise = client.fetch<PortableTextBlock[]>(`*[_type == "simpleBlock" && _id == $id][0].body`, {id: '6468a09e-ce29-48d3-8b36-4b058366e6b5'})
56+
const client = createClient({
57+
projectId: 'ppsg7ml5',
58+
dataset: 'test',
59+
apiVersion: '2025-12-09',
60+
useCdn: true,
61+
perspective: 'published',
62+
})
63+
const promise = client.fetch<PortableTextBlock[]>(
64+
`*[_type == "simpleBlock" && _id == $id][0].body`,
65+
{id: '6468a09e-ce29-48d3-8b36-4b058366e6b5'},
66+
)
5467

5568
function Demo() {
5669
const blocks = use(promise)
@@ -62,7 +75,7 @@ function Demo() {
6275
// setCount(count + 100)
6376
return undefined
6477
}
65-
const raf = requestAnimationFrame(() => setCount( count + 30))
78+
const raf = requestAnimationFrame(() => setCount(count + 30))
6679
return () => cancelAnimationFrame(raf)
6780
}, [showingEverything, count])
6881
return <PortableText value={blocksToShow} components={ptComponents} />

src/react-portable-text.tsx

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,12 @@ function RenderList({
203203
})
204204
}
205205

206-
return (
206+
return useMemo(
207+
() => (
207208
<List value={node} index={index} isInline={false} renderNode={renderNode}>
208209
{children}
209210
</List>
210-
)
211+
), [children, index, List, node, renderNode])
211212
}
212213

213214
function RenderListItem({
@@ -248,10 +249,13 @@ function RenderListItem({
248249
})
249250
}
250251

251-
return (
252-
<Li value={node} index={index} isInline={false} renderNode={renderNode}>
253-
{children}
254-
</Li>
252+
return useMemo(
253+
() => (
254+
<Li value={node} index={index} isInline={false} renderNode={renderNode}>
255+
{children}
256+
</Li>
257+
),
258+
[children, index, Li, node, renderNode],
255259
)
256260
}
257261

@@ -284,16 +288,19 @@ function RenderSpan({
284288
})
285289
}
286290

287-
return (
288-
<Span
289-
text={spanToPlainText(node)}
290-
value={markDef}
291-
markType={markType}
292-
markKey={markKey}
293-
renderNode={renderNode}
294-
>
295-
{children}
296-
</Span>
291+
return useMemo(
292+
() => (
293+
<Span
294+
text={spanToPlainText(node)}
295+
value={markDef}
296+
markType={markType}
297+
markKey={markKey}
298+
renderNode={renderNode}
299+
>
300+
{children}
301+
</Span>
302+
),
303+
[children, markDef, markKey, markType, node, renderNode, Span],
297304
)
298305
}
299306

@@ -311,9 +318,11 @@ function RenderCustomBlock({
311318
isInline: boolean
312319
}) {
313320
const Node = components.types[node._type]
314-
return Node ? (
315-
<Node value={node} isInline={isInline} index={index} renderNode={renderNode} />
316-
) : null
321+
return useMemo(
322+
() =>
323+
Node ? <Node value={node} isInline={isInline} index={index} renderNode={renderNode} /> : null,
324+
[index, isInline, node, Node, renderNode],
325+
)
317326
}
318327

319328
function RenderBlock({

0 commit comments

Comments
 (0)