【发布时间】:2018-12-01 05:04:32
【问题描述】:
我正在从 API 中提取 JSON 数据。 JSON 包含来自社交媒体网站 Steemit.com 的博客文章。帖子在标记中,我想从 API 中获取标记,将其转换为常规 HTML,并将其显示在 React 组件中。
这是我的渲染函数。 API 数据在componentDidMount 中调用,由深度嵌套的对象组成。打扰一下。
render() {
const posts = Object.keys(this.state.myPosts);
let page = posts.map((post, i) => {
return (
this.state.myPosts[posts[i]]["category"] === "utopian-io"
?
<div className="content-container">
<p>
<strong>Author: {this.state.myPosts[posts[i]]["author"]}</strong>
</p>
<p>
<strong>Title: {this.state.myPosts[posts[i]]["title"]}</strong>
</p>
<p>
<strong>Payout: {this.state.myPosts[posts[i]]["total_payout_value"]}</strong>
</p>
<p>{this.state.myPosts[posts[i]]["created"]}</p>
<p><span>Category: </span>{this.state.myPosts[posts[i]]["category"]}</p>
// Successfully renders the markup, but I want to convert this to HTML so it displays as HTML
<p>{this.state.myPosts[posts[i]]["body"]}</p>
// I have tried the following but I get a "not a string" console.error or the browser just crashes
<Markdown>{this.state.myPosts[posts[i]]["body"]}</Markdown>
</div>
:
null
);
});
return (
<div>
{page}
</div>
);
}
}
Markdown-to-JSX 包成功导入,使用<Markdown></Markdown> 标签调用它是合适的使用方式。问题是它需要一个字符串,当我通过它this.state.myPosts[posts[i]]["body"] 时它不起作用。我尝试使用toString() 和JSON.stringify() 将数据转换为字符串,但我无法弄清楚。
如何将 API 中的 JSON 标记数据作为字符串传递给 Markdown-to-JSX?
换句话说,我认为将this.state.myPosts[posts[i]]["body"] 转换为字符串应该可以解决这个问题,但我不知道该怎么做。
【问题讨论】:
标签: javascript html reactjs markdown markup