【发布时间】:2020-02-12 09:05:04
【问题描述】:
我有两个存储句子数组的状态,假设说sentencesHard state,另一个存储单词数组criticalWords state。我需要在sentenceHard 的句子中从criticalWords 中突出这些词。现在,如果句子中包含单词,它会突出显示整个句子,但我只需要突出显示该特定单词而不是整个句子。
例如,“Configuringreact-redux 是complicated”
现在,我的代码如下所示:
states={
criticalWords:["configuring", "complicated"],
sentencesHard: ["configuring react-redux is complicated"]
}
{this.state.sentencesHard.map((sentence) => (
<span style={{ backgroundColor: this.state.criticalWords.some(word => sentence.includes(word)) ? '#e4b9b9' : 'initial' }}>
{sentence}
</span>
))
}
【问题讨论】:
标签: javascript reactjs