如何将长字符串拆分为多行字符串,但该字符串是javascript对象内部的值
我正在为一个网站创建一个常见问题解答页面,我决定创建一个包含每个问题和答案的对象数组,然后通过该数组进行映射以使我的反应代码看起来更干净。
有些答案是段落,但我想将一些段落分成多个较小的段落。
我已经尝试过在反引号内使用 \n 以及在单引号和双引号内使用 \n ,但该段落不会在浏览器中分成多行。它是否与对象内部有关,这就是为什么它不会添加带有 \n 的新行?
这是我的常见问题解答对象:
export const faqData = [{
question: "Can we make this product personalized?",
answer: "Yes, all products can be personalized. Please visit our shop page for
all personalized options.\n You can view the shop page here."
},
这是我的组件:
import React from "react";
import Title from "./title/Title";
import Questions from "./faqs/Questions";
import { faqData } from "./utils/faqs";
const FAQ = () => {
return (
<div className="Div">
<Title />
{faqData.map(({ question, answer }) => (
<Questions question={question} answer={answer} />
))}
</div>
);
};
export default FAQ;
I am creating an FAQ page for a site, and instead of statically typing out every question and answer into each component, I decided to create an array of objects with every question and answer inside of it, and then map through the array to make my react code look cleaner.
Some of the answers are paragraphs, but I want to split up some of the paragraphs into multiple smaller paragraphs.
I've tried \n within backticks and \n within single and double quotes but the paragraph does not split up into multiple lines in the browser. Does it have something to do with being inside of an object and that's why it won't add a new line with \n?
Here is my FAQ object :
export const faqData = [{
question: "Can we make this product personalized?",
answer: "Yes, all products can be personalized. Please visit our shop page for
all personalized options.\n You can view the shop page here."
},
Here is my component :
import React from "react";
import Title from "./title/Title";
import Questions from "./faqs/Questions";
import { faqData } from "./utils/faqs";
const FAQ = () => {
return (
<div className="Div">
<Title />
{faqData.map(({ question, answer }) => (
<Questions question={question} answer={answer} />
))}
</div>
);
};
export default FAQ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
遗憾的是,我们无法看到您如何在显示的代码中拆分
Questions
组件中的字符串。它应该是string.split('\n')
上相当简单的映射。在
Questions
组件的返回中,它应该类似于以下内容:这会将
answer
分解为由\n
的,然后将其映射到单独的段落标签中。Unfortunately we can't see how you're splitting the string in the
Questions
component with your code shown. It should be a fairly simple mapping on thestring.split('\n')
.In the return of your
Questions
component, it should look something similar to:This will break the
answer
into an array of strings split up by\n
's, then map it into individual paragraph tags.默认情况下,HTML 会折叠空格。您可以使用
white-space
CSS 属性更改此行为。在这种情况下,white-space: pre-line
可能就是您想要的。By default, HTML collapses whitespace. You can change this behaviour with the
white-space
CSS property. In this case,white-space: pre-line
might be what you want.对于显示多行而不是
\n
的不同解决方案,您可以将answer
设置为 HTML 格式(以使用
)并使用dangerouslySetInnerHTML
(警告)完整演示
<一个href="https://codesandbox.io/s/angry-haibt-z4u9bg?fontsize=14&hidenavigation=1&theme=dark" rel="nofollow noreferrer">
For a different solution to display multiline instead of
\n
, you could format youranswer
in HTML (to use<br />
) and usedangerouslySetInnerHTML
(warning)Full demo