gpt4 book ai didi

javascript - 如何将 json 字符串中的某些文本设为粗体?

转载 作者:行者123 更新时间:2023-12-02 22:36:37 26 4
gpt4 key购买 nike

我想在 react 中连接两个字符串,使第一个字符串显示为粗体,第二个字符串则不显示。我想要在 JSON 文件中加粗字符串,并且想要连接来自后端 API 调用的字符串。这是我的设置:

这是一个 JSON 文件:

  { stuff: {
stuffIWantBolded: "bold text"
}
}

我的前端看起来像这样:

  render() {
// props for this component in which I'm rendering SomeComponent (see below)
const { data } = this.props
const { theStringFromBackend } = data
// a method to get the string that is working for me
const stuff = this.getTheStringFromTheJSON();
const textIWantToDisplay = `${stuff.stuffIWantBolded} ${theStringFromBackend}`;
return (
<SomeComponent someProp={textIWantToDisplay} />
);
};

这两个字符串成功连接起来。我尝试过使用 .bold()stuff.stuffIWantBolded 末尾,但这显然不起作用,因为字符串呈现为 <b>bold text</b> the string from backend (假设这就是后端的字符串所说的内容),显式写出 HTML 标签,而不是呈现实际的粗体文本。我有什么遗漏的吗?我不认为可以在 JSON 中将字符串加粗...也许我需要使用正则表达式?任何帮助将不胜感激。

最佳答案

这个怎么样:

return (
<>
<span style={{fontWeight: "bold"}}>{stuff.stuffIWantBolded}</span>
<span>{theStringFromBackend}</span>
</>
);

<></>有效地允许您从一个渲染函数返回多个项目。

关于javascript - 如何将 json 字符串中的某些文本设为粗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58720905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com