首页 > 解决方案 > ReactJS:从 MySQL 获取 mediumtext 并查看格式化文本

问题描述

我有一个非常具体的问题,所以:我的 MySQL 数据库中有一个中等文本。例如(带断线):

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

我现在想使用 get HTTP 方法获取此文本,并在我的站点显示Lorem Ipsum中以粗体显示

有可能得到什么吗?我将 ReactJS 与 Redux + Redux-Saga 一起使用

或者可能在数据库中,我可以在 html中插入中等文本,但是如何在前端显示呢?

标签: mysqlreactjs

解决方案


存储 html 并用于dangerouslySetInnerHTML渲染它。阅读这篇文章,您可以更深入地了解它。

const text = '<strong>Lorem ipsum</strong> dolor sit';

const App = ({html}) => (
  <div dangerouslySetInnerHTML={{ __html: html }} />
)

ReactDOM.render(<App html={text} />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>


推荐阅读