Search highlights from customTextRenderer never appear in react-pdf
Your error
TextLayer styles not found. Read more: https://github.com/wojtekmaj/react-pdf#support-for-text-layer2 more lines it can showFix it fast
Most common cause, 1 of 4TextLayer.css is not imported
Import the stylesheet once, in the component that renders <Page> or in the app entry.
import { Document, Page } from 'react-pdf';
import 'react-pdf/dist/Page/TextLayer.css';
import 'react-pdf/dist/Page/AnnotationLayer.css';Short answer
When react-pdf's customTextRenderer highlight shows nothing, the text layer is usually unstyled: since react-pdf 6, TextLayer.css must be imported by hand. The renderer must also return an HTML string, not JSX, and a plain string pattern marks only the first hit per text item. Applies to react-pdf 6 and later, including 11.x on React 19.
Which cause do you have?
Open the page with the PDF, then paste these three lines into the browser console. An empty first value points to cause 1. A span count of 0 points to cause 4. Spans present but zero marks points to cause 2 or 3.
getComputedStyle(document.body).getPropertyValue('--react-pdf-text-layer')
document.querySelectorAll('.react-pdf__Page__textContent span').length
document.querySelectorAll('.react-pdf__Page__textContent mark').lengthThen find the line you see in this list and open its fix:
- 1TextLayer.css is not imported
The console prints TextLayer styles not found, and the first command in the first step returns an empty string - 2The renderer returns JSX instead of an HTML string
The renderer was copied from an older tutorial and returns <mark>{value}</mark> as JSX; zero <mark> elements exist in the layer - 3A plain string pattern marks only the first hit in each text item
Some words light up, but a second match on the same line, or a match in other letter case, stays plain - 4No text layer is rendered for the page
The span count from the first step is 0, and an onGetTextSuccess callback on the page never runs
What does the error look like?
TextLayer styles not found. Read more: https://github.com/wojtekmaj/react-pdf#support-for-text-layercustomTextRenderer returns <mark> tags but no highlight is visible on the pageOnly the first matching word in a line is highlighted
Cause 1 of 4
TextLayer.css is not imported
You seeThe console prints TextLayer styles not found, and the first command in the first step returns an empty string
Check
A customTextRenderer highlight that never lines up with the words means the text layer has no stylesheet. React-PDF 6.0.0 stopped loading TextLayer.css on its own, so the layer's spans lose the absolute positioning and transparent colour that place them over the drawn page.
grep -rn "TextLayer.css" src/Fix
Import the stylesheet once, in the component that renders <Page> or in the app entry. Add AnnotationLayer.css too if the PDF has links.
import { Document, Page } from 'react-pdf';
import 'react-pdf/dist/Page/TextLayer.css';
import 'react-pdf/dist/Page/AnnotationLayer.css';Verify
Reload: the warning is gone and the CSS variable that the stylesheet defines reads 1.
getComputedStyle(document.body).getPropertyValue('--react-pdf-text-layer') // "1"Confirmed by raw.githubusercontent.com
Cause 2 of 4
The renderer returns JSX instead of an HTML string
You seeThe renderer was copied from an older tutorial and returns <mark>{value}</mark> as JSX; zero <mark> elements exist in the layer
Check
A customTextRenderer that returns React elements cannot highlight anything on current versions. React-PDF 6.0.0 dropped support for React content in this prop, and TextLayer now writes the returned value into each span as HTML, so an element object is turned into the text [object Object] rather than a mark.
grep -rn -A4 "customTextRenderer" src/Fix
Return a string that contains the HTML. React-PDF filters out unsafe tags such as script and iframe as a best effort, but a plain mark element passes through.
const textRenderer = useCallback(
({ str }) => str.replace('invoice', (hit) => `<mark>${hit}</mark>`),
[],
);
<Page pageNumber={1} customTextRenderer={textRenderer} />Verify
The mark count from the first step is above 0 for a page that contains the word.
document.querySelectorAll('.react-pdf__Page__textContent mark').lengthConfirmed by github.com
Cause 3 of 4
A plain string pattern marks only the first hit in each text item
You seeSome words light up, but a second match on the same line, or a match in other letter case, stays plain
Check
The highlight recipe in the react-pdf wiki passes the search text straight to String.prototype.replace. With a string pattern, replace changes only the first occurrence and compares case exactly, so a repeated word inside one text item is skipped and Invoice does not match invoice.
grep -rn "\.replace(" src/ | grep -i "mark"Fix
Build a regular expression with the g and i flags from the escaped search text. RegExp.escape has been Baseline across current browsers since May 2025. Return the text unchanged while the search box is empty.
const textRenderer = useCallback(
({ str }) => {
if (!searchText) return str;
const pattern = new RegExp(RegExp.escape(searchText), 'gi');
return str.replace(pattern, (hit) => `<mark>${hit}</mark>`);
},
[searchText],
);Verify
Search for a word that appears twice in one line in different case: both copies are marked.
document.querySelectorAll('.react-pdf__Page__textContent mark').lengthConfirmed by developer.mozilla.org
Cause 4 of 4
No text layer is rendered for the page
You seeThe span count from the first step is 0, and an onGetTextSuccess callback on the page never runs
Check
customTextRenderer only changes the text layer, so it has nothing to work on when renderTextLayer is false or when the page is drawn with Thumbnail. The react-pdf README states that Thumbnail renders neither the annotation layer nor the text layer and does not accept customTextRenderer.
grep -rn "renderTextLayer\|<Thumbnail" src/Fix
Render the page with <Page> and leave renderTextLayer at its default of true. Log the number of text items while you debug.
<Page
pageNumber={1}
customTextRenderer={textRenderer}
onGetTextSuccess={({ items }) => console.log('text items:', items.length)}
/>Verify
The console logs a non-zero item count, and the span count from the first step is above 0.
document.querySelectorAll('.react-pdf__Page__textContent span').lengthConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Import TextLayer.css in the same file that renders <Page>, so moving the viewer to another route cannot leave the styles behind.
- Keep the renderer in useCallback with the search text as its only dependency, as the react-pdf recipe does, so the layer re-renders only when the query changes.
- Add a component test with a two-word sample that asserts both words are marked, which catches a return to a plain string pattern.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.