最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactMaterialUI: How to have whitespace from string be respected? - Stack Overflow

programmeradmin1浏览0评论

Attempting to use Typography from Material UI (/)

The goal is to have new lines and spaces from the saved string to be respected.

Such that an example which has leading spaces and new lines would be rendered as:

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

If I just use <Typography>{val}</Typography> then value gets rendered in one line such as:

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

Adding {{ whiteSpace: 'pre-line' }} makes Typography at least respect the new lines:

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

Rendering the string as

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

But how do we have the ponent respect the new line and leading spaces?

Attempting to use Typography from Material UI (https://material-ui./api/typography/)

The goal is to have new lines and spaces from the saved string to be respected.

Such that an example which has leading spaces and new lines would be rendered as:

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

If I just use <Typography>{val}</Typography> then value gets rendered in one line such as:

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

Adding {{ whiteSpace: 'pre-line' }} makes Typography at least respect the new lines:

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

Rendering the string as

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

But how do we have the ponent respect the new line and leading spaces?

Share Improve this question asked May 25, 2021 at 23:21 user7858768user7858768 1,0561 gold badge15 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I know it's super late and not useful in particular to you anymore, but in case anyone needs a solution for this problem: style={{ whiteSpace: "pre-wrap" }}

From w3schools: "Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks"

With "pre-line" whitespaces collapse into a single whitespace

You could use a preformatted text element instead of <Typography />

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

评论列表(0)

  1. 暂无评论