(This is all in a class-based component)
I have a component that essentially handles dumping out a text file line by line onto the page. Each sentence of that text file is separated by a line and within its own <p>
element like so:
{this.state.contract.text.map((line, index) => (
<p key={index}>{line}
<Highlighter
highlightClassName="MyHC"
highlightStyle={{backgroundColor: "#ffd500"}}
searchWords={queryWords}
autoEscape={true}
textToHighlight={line}/>
</p>
))}
I am curious how I could go about scrolling to a specific bit of text within this large text dump, specifically words that appear in queryWords which is just a string. I would have to do this after the full page is rendered I'm guessing but not sure where I'd make the call to a function that could do something like this with that queryWords variable in mind. What Ideally happens is the text gets highlighted according to the queryWords all throughout the text and then the window scrolls to the first instance of <p>
that is highlighted.
One approach I had was to just take this queryWords and doing something with this:
$(`*:contains('${queryWords}'):first`);
but this gave me some trouble. Is there any other methods of scrolling to a specific string of text that has been rendered (and where would I call a function to do this, after the first code snippet or within componentDidMount)?