How to convert it to html element?
P粉248602298
P粉248602298 2023-09-08 21:58:04
0
1
653

I'm trying to create a function that highlights words in a sentence. Here is the corresponding code.

highlightText = () => {
        const {
            questionItem,
        } = this.props;
        const words = questionItem.split(/\s+/);
        const highlighted = words.map((word, index) => (
            <span key={index} className="highlighted-word">{word}</span>
        ));
        const text = highlighted.join(' ');
        console.log(text);
        console.log({ highlighted });
        return highlighted;
    }

Here, if I console "text", I get [Object object], so I return "highlight" and then use reduce to join it.

const text3 = <p>{this.highlightText().reduce((prev, curr) => [prev, ' ', curr])}</p>;

I want to use this "text3" in the component. The problem is that I get "text3" as

I want it to be an HTML element so I can use it in my component. How can I convert it? Also, if I display

{this.highlightText().reduce((prev, curr) => [prev, ' ', curr])}

directly in the web page instead of Using it in my component works fine.

P粉248602298
P粉248602298

reply all(1)
P粉211600174

Just create a new component with props. Try this...

export default function HighlightText({text}) {
 return <span className="Add_HighlightText_Class" >{text}</span>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template