首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React/Javascript显示API数据显示硬编码链接而不是超链接?

React/Javascript显示API数据显示硬编码链接而不是超链接?
EN

Stack Overflow用户
提问于 2021-02-02 06:44:20
回答 1查看 49关注 0票数 0

所以我把这个API搞得乱七八糟,对于描述,它有像<a href="/">Bunch of words</a>一样的硬编码链接,所以它在我的浏览器上准确地显示了这一点。我如何在我的应用程序中显示描述以使其看起来正常?

以下是https://api.coingecko.com/api/v3/coins/bitcoin接口

这只是一种简单的方式,我得到了描述来显示

代码语言:javascript
复制
const Tokens = ({coin}) => {
 return (
      <p>{coin.description.en}</p>
  )
 }

这将最终在浏览器上显示所有的a标记,而不是将它们转换为可点击的链接

代码语言:javascript
复制
  <a href=\"https://www.coingecko.com/en/coins/peercoin\">Peercoin</a>, <a href=\"https://www.coingecko.com/en/coins/primecoin\">Primecoin</a>, and so on.\r\n\r\nThe cryptocurrency then took off with the innovation of the turing-complete smart contract by <a href=\"https://www.coingecko.com/en/coins/ethereum\">Ethereum</a> which led to the development of other amazing projects such as <a href=\"https://www.coingecko.com/en/coins/eos\">EOS</a>, <a href=\"https://www.coingecko.com/en/coins/tron\">Tron</a>, and even crypto-collectibles such as <a href=\"https://www.coingecko.com/buzz/ethereum-still-king-dapps-cryptokitties-need-1-billion-on-eos\">CryptoKitties</a>.",

有没有办法显示描述,让它看起来像一个普通的带有超链接的段落,而不是字面上显示硬编码的标签?

此外,如果我只想像前两句话那样显示,我该如何剪切段落的其余部分?

EN

回答 1

Stack Overflow用户

发布于 2021-02-02 07:40:26

正如雅各布所说,您可以通过使用插值字符串文字来使用dangerouslySetInnerHTML属性。

代码语言:javascript
复制
const apiResponse = `<a href=\"https://www.coingecko.com/en/coins/peercoin\">Peercoin</a>, <a href=\"https://www.coingecko.com/en/coins/primecoin\">Primecoin</a>, and so on.\r\n\r\nThe cryptocurrency then took off with the innovation of the turing-complete smart contract by <a href=\"https://www.coingecko.com/en/coins/ethereum\">Ethereum</a> which led to the development of other amazing projects such as <a href=\"https://www.coingecko.com/en/coins/eos\">EOS</a>, <a href=\"https://www.coingecko.com/en/coins/tron\">Tron</a>, and even crypto-collectibles such as <a href=\"https://www.coingecko.com/buzz/ethereum-still-king-dapps-cryptokitties-need-1-billion-on-eos\">CryptoKitties</a>."`
...
<p dangerouslySetInnerHTML={{__html: apiResponse}}></p>.

但是,\r\n不能被超文本标记语言所理解。您可以将这些空格字符替换为HTML可以理解的unicode转义序列,如下所示:

代码语言:javascript
复制
const cleanedAPIResponse = apiResponse.replace("\n", "<br\>").replace("\r", "\u000D");
...
<p dangerouslySetInnerHTML={{__html: cleanedAPIResponse}}></p>.

注意:对这些替代方案不是很确定。仅供参考,\r被称为“回车”。

如果你只想要前两句话,一个想法是搜索‘’的第二个实例。在API响应中。然后,您可以截断字符串文字的其余部分,并根据从左到右排列的标记的外观附加适当的结束标记,这些标记在字符串中还没有匹配的结束标记。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66001207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档