【问题标题】:React/MaterialUI: How to have whitespace from string be respected?React/Material UI:如何尊重字符串中的空格?
【发布时间】:2021-05-25 23:21:50
【问题描述】:

尝试使用 Material UI 中的排版 (https://material-ui.com/api/typography/)

我们的目标是在保存的字符串中保留新的行和空格。

这样一个具有前导空格和换行符的示例将呈现为:

const svg = d3.select("#chart-area")
   .append("svg")  
   .attr("width", 400)
   .attr("height", 400)

如果我只使用<Typography>{val}</Typography>,那么值会在一行中呈现,例如:

const svg = d3.select("#chart-area") .append("svg") .attr("width", 400) .attr("height", 400)

添加 {{ whiteSpace: 'pre-line' }} 使 Typography 至少尊重新行:

<Typography style={{ whiteSpace: 'pre-line' }}>{val}</Typography>

将字符串渲染为

const svg = d3.select("#chart-area")
.append("svg")
.attr("width", 400)
.attr("height", 400)

但是我们如何让组件尊重新行和前导空格?

【问题讨论】:

    标签: javascript html reactjs material-ui


    【解决方案1】:

    您可以使用preformatted text element 代替&lt;Typography /&gt;

    <pre>
      const svg = d3.select("#chart-area")
    .append("svg")
    .attr("width", 400)
    .attr("height", 400)
    </pre>
    

    【讨论】:

      猜你喜欢
      • 2011-12-26
      • 2020-07-27
      • 2020-06-23
      • 2021-03-24
      • 2016-09-16
      • 2018-12-24
      • 2019-10-07
      • 2015-10-04
      • 2021-02-11
      相关资源
      最近更新 更多