【发布时间】:2021-10-09 15:48:44
【问题描述】:
我使用下面的数据在 React 中作为 props 传递。一切正常,但我只需要在文本属性中加粗“目标受众”一词。有没有办法做到这一点?
const SlideData = [
{
index: 1,
title: "Target Audience",
text: [
"The target audience for this course is anyone who is assigned roles as a HR Employee Maintainer...",
],
image: {
src: targetAudience,
width: imageSize,
},
},
index: 2,
title: "Reporting ",
text: [
"Reporting Manager is designated to...",
],
image: {
src: reporting,
width: imageSize,
},
},
]
export default SlideData
添加渲染组件
const TextSlide = ({ title, text = [], list, image }) => {
return (
<>
<div className="slide">
<div className="standard-grid">
<span className="slide-title title">{title}</span>
<div className="content">
{text.map((t, i) => (
<p key={i} className="text">
{t}
</p>
))}
</div>
{image ? <img className="picture" src={image.src} style={{ maxWidth: image.width }} alt="image" /> : null}
</div>
</div>
</>
);
};
export default TextSlide;
``
【问题讨论】:
-
如何渲染文本?
-
粗体字与演示相关,而您的
SlideData与数据相关;你应该尽量避免混合这两层。无论如何,您可以处理此问题的一种方法是使用类似降价的方法并将"The target audience for this course is ..."更改为"The #target audience# for this course is ...,然后在渲染期间将#替换为相关标签。 -
如果有帮助,我添加了渲染位置
-
好吧,我绝对不希望将它们混合在一起 @secan ,我考虑过创建一个包含粗体字符串“目标受众”的 const,然后将其导入 text: property via string literal?但那样我就会有很多……
标签: javascript arrays reactjs string object