如何将长字符串拆分为多行字符串,但该字符串是javascript对象内部的值

发布于 2025-01-11 17:59:28 字数 963 浏览 0 评论 0原文

我正在为一个网站创建一个常见问题解答页面,我决定创建一个包含每个问题和答案的对象数组,然后通过该数组进行映射以使我的反应代码看起来更干净。

有些答案是段落,但我想将一些段落分成多个较小的段落。

我已经尝试过在反引号内使用 \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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒人说梦 2025-01-18 17:59:28

遗憾的是,我们无法看到您如何在显示的代码中拆分 Questions 组件中的字符串。它应该是 string.split('\n') 上相当简单的映射。

Questions 组件的返回中,它应该类似于以下内容:

return (
   <div className="Questions">
      <h2>${props.question}</h2>
      {
         props.answer.split('\n').map((paragraph) => {
            return <p>{paragraph}</p>;
         }
      }
   </div>
)

这会将 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 the string.split('\n').

In the return of your Questions component, it should look something similar to:

return (
   <div className="Questions">
      <h2>${props.question}</h2>
      {
         props.answer.split('\n').map((paragraph) => {
            return <p>{paragraph}</p>;
         }
      }
   </div>
)

This will break the answer into an array of strings split up by \n's, then map it into individual paragraph tags.

溺孤伤于心 2025-01-18 17:59:28

默认情况下,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.

人间不值得 2025-01-18 17:59:28

对于显示多行而不是 \n 的不同解决方案,您可以将 answer 设置为 HTML 格式(以使用
)并使用 dangerouslySetInnerHTML警告

const faqData = [
  {
    question: "1 Can we make this product personalized?",
    answer: "Yes, all products can be personalized."
  },
  {
    question: "2 Can we make this product personalized?",
    answer:
      "Yes, all products can be personalized. Please visit our shop page for all personalized options.<br /> You can view the shop page here."
  }
];

const Title = () => <h1>FAQ</h1>;

const Questions = ({ question, answer }) => (
  <div>
    <h3>{question}</h3>
    <p dangerouslySetInnerHTML={{ __html: answer }} />
  </div>
);

完整演示

<一个href="https://codesandbox.io/s/angry-haibt-z4u9bg?fontsize=14&hidenavigation=1&theme=dark" rel="nofollow noreferrer">编辑angry-haibt-z4u9bg

For a different solution to display multiline instead of \n, you could format your answer in HTML (to use <br />) and use dangerouslySetInnerHTML (warning)

const faqData = [
  {
    question: "1 Can we make this product personalized?",
    answer: "Yes, all products can be personalized."
  },
  {
    question: "2 Can we make this product personalized?",
    answer:
      "Yes, all products can be personalized. Please visit our shop page for all personalized options.<br /> You can view the shop page here."
  }
];

const Title = () => <h1>FAQ</h1>;

const Questions = ({ question, answer }) => (
  <div>
    <h3>{question}</h3>
    <p dangerouslySetInnerHTML={{ __html: answer }} />
  </div>
);

Full demo

Edit angry-haibt-z4u9bg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文