php - 此 React 应用程序中的实际数据和演示数据不同

标签 php mysql reactjs

我在 React 中渲染时是否遗漏了一些东西?

数据库中保存的数据为:

actual data
is
this

用React渲染后,数据变成这样:

actual data is this

我检查了所有数据点,从表单到使用 Ajax 发送并添加到数据库。数据与正在发送的数据相同。并且不存在任何符合 display:inline 规则的 CSS 内容。

最佳答案

给定字符串中的换行符 ( '\n' ) 不会导致呈现的 HTMl 中出现换行符。您可以添加<br/>标签放在行尾,或者将它们包裹在 <p> 中标签:

App.jsx

const React = require('react');
const reactDOM = require('react-dom');

function App(props) {
  const breaks = props.string2.split('\n').map((line) => <span>{line}<br /></span>);
  const paragraphs = props.string3.split('\n').map((line) => <p>{line}</p>);

  return (
    <div>
      <div>
        {props.string1}
      </div>
      <div>
        {breaks}
      </div>
      <div>
        {paragraphs}
      </div>
    </div>
  );
}

const string1 = 'will\n all\n be\n one\n line';
const string2 = 'will\n be\n on\n different\n lines\n with\n breaks';
const string3 = 'will\n be\n on\n different\n lines\n with\n paragraphs';

reactDOM.render(<App string1={string1} string2={string2} string3={string3} />, document.getElementById('app'));

这将呈现:

will all be one line
will
be
on
different
lines
with
breaks
will

be

on

different

lines

with

paragraphs

关于php - 此 React 应用程序中的实际数据和演示数据不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57439368/

相关文章:

php - 如何在php中查看mysql查询结果

mysql - 100K以上记录的mysql表如何正常添加一列?

javascript - ReactJS:如何水平渲染列

javascript - 如何制作React原生路由

php - 如何使异常消息在 Symfony 4 用户检查器中可翻译?

php - Services_Twilio_TinyHttpException 错误 PHP

mysql - MySQL最好的双重替代品是什么?

javascript - 如何在样式组件中使用 for 循环?

php - CodeIgniter - 将现有类与 CodeIgniter 一起使用,直接包含它们有什么问题吗?

php - Laravel 使用该行的 id 显示一个数据库表中的数据