javascript - React/MaterialUI:如何尊重字符串中的空格?
问题描述
尝试从 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)
但是我们如何让组件尊重新行和前导空格?
解决方案
您可以使用预先格式化的文本元素而不是<Typography />
<pre>
const svg = d3.select("#chart-area")
.append("svg")
.attr("width", 400)
.attr("height", 400)
</pre>
推荐阅读
- php - PHP:list() 在 PhpStorm 中给出未使用的局部变量错误
- javascript - 在 Reactjs 中,我如何为复选框做自定义消息。我看到这个问题有很多答案适用于javascript而不是reactjs
- java - 在底部导航抽屉中加载带有标记列表的地图片段
- python - Pandas - 添加交替值的列
- ruby-on-rails - rails 5 API 低级缓存
- java - 如何将小黄瓜步骤定义数据映射到 Cucumber 中的 Java 对象?
- java - 类对象关系如何工作?
- google-apps-script - ScriptProperties 和 UserProperties 已被弃用
- python - 用例如 UTF-8 编码的文本文件中的字典替换单词
- python - AccessToken 匹配查询不存在。使用 Postman 匹配查询不存在