如何將其轉換為 html 元素?
P粉248602298
P粉248602298 2023-09-08 21:58:04
0
1
385

我正在嘗試建立一個突出顯示句子中單字的函數。這是對應的程式碼。

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;
    }

在這裡,如果我控制台“文字”,我會得到[Object object],所以我會返回“突出顯示”,然後使用reduce加入它。

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

我想在元件中使用這個「text3」。問題是我得到的「text3」為

我希望它是一個 HTML 元素,以便我可以在我的元件中使用它。我該如何轉換它? 另外,如果我直接在網頁中顯示

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

而不是在我的組件中使用它工作正常。

P粉248602298
P粉248602298

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!