You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TSX文本换行与段落显示问题求助

你的代码里description字段的写法存在错误——用了模板字符串(``)却错误混入了普通字符串拼接的" +和"\n" +语法,这会导致\n要么被当成普通字符显示,要么完全不生效。以下是具体的修复方案:


第一步:修正description的字符串格式

删除多余的" +和"\n" +,模板字符串里直接用换行(或\n)分隔段落即可:

export const aboutData: About = {
  profileImageSrc: profilepic,
  description: `Hello! thanks for taking the time to visit my professional website. My name is Jeremiah Dunphy (He/Him) and I work in a Strategic Senior Human Resources at CW Resources Inc. I have always been passionate about helping people and organizations reach their full potential, and I have found a perfect outlet for this passion in the field of HR and People Management.

I began my career in HR almost a decade ago and have since gained a wide range of experience in various industries and organizations. I have held HR positions in both small and large companies, giving me a well-rounded understanding of the unique challenges and opportunities that different types of organizations face when it comes to managing and developing their human capital.

I hold a Bachelors in Human Resources and a Masters in Business Administration. I am currently obtaining my doctorate in Human Resources and Business Management.In my current role as HR Director, I am responsible for overseeing all aspects of human resources management in over 20 states.

Outside of work, I enjoy spending time with my Family, Coding, Data, Running, Website and Game Development. I am also an active member of the Society for Human Resource Management and am always looking for ways to stay up-to-date on the latest HR trends and best practices.

As a Combat Veteran who has served on two tours of duty, I know firsthand the sacrifices that our military members make to protect our country. That is why I am committed to giving back to the veteran community through my volunteer work at the VA. Whether it is providing support and companionship to Veterans during hospital stays, helping with paperwork and benefits, or just being a listening ear, I am dedicated to doing whatever I can to assist and support our Veterans. It is my way of honoring their service and sacrifices, and I am grateful for the opportunity to give back to those who have given so much for our country.

While Volunteering, I have had the opportunity to assist Veterans with a variety of tasks and needs. Whether it is providing support and companionship during hospital stays, helping with paperwork and benefits, or just being a listening ear, I am committed to doing whatever I can to help our Veterans.

I am grateful for the opportunity to give back to those who have given so much for our country, and I am committed to continuing to support and assist Veterans in any way I can. It is a small way to show my appreciation and gratitude for their service and sacrifice.

Thank you for taking the time to learn more about me. I am excited to bring my skills and experience to bear in helping organizations and individuals reach their full potential.`,
  aboutItems: [
    {label: 'Location', text: 'Hartford, CT', Icon: MapIcon},
    {label: 'Age', text: '27', Icon: CalendarIcon},
    {label: 'Nationality', text: '', Icon: FlagIcon},
    {label: 'Interests', text: 'People, Computer Science, Law and Psycology', Icon: SparklesIcon},
    {label: 'Studying', text: 'Docotorate in Human Resources and Business Management - Capella University', Icon: AcademicCapIcon},
    {label: 'Employment', text: 'Cw Resources Inc.', Icon: OfficeBuildingIcon},
  ],
};

第二步:在TSX中实现分段换行

有两种实用方法可选:

方法1:用CSS解析换行符

给渲染文本的元素添加white-space: pre-line样式,浏览器会自动把\n解析成换行,同时合并多余空白:

import { aboutData } from './your-file-path';

export default function AboutSection() {
  return (
    <div className="description-text">
      {aboutData.description}
    </div>
  );
}

对应的CSS:

.description-text {
  white-space: pre-line;
  line-height: 1.6;
  margin-bottom: 1rem;
}

方法2:拆分为<p>标签(更语义化)

把字符串按\n\n(两个换行对应段落分隔)拆分成数组,遍历渲染为<p>标签:

import { aboutData } from './your-file-path';

export default function AboutSection() {
  const paragraphs = aboutData.description.split('\n\n').filter(p => p.trim());
  
  return (
    <div className="description-container">
      {paragraphs.map((para, index) => (
        <p key={index} className="paragraph">
          {para}
        </p>
      ))}
    </div>
  );
}

对应的CSS:

.description-container .paragraph {
  margin-bottom: 1.5rem;
  line-height: 1.6;
}

内容的提问来源于stack exchange,提问作者Jeremiah Dunphy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 10:55:18