在React代码行中添加 ap 标签的位置

发布于 2025-01-20 11:22:38 字数 1880 浏览 2 评论 0原文

我有两个组件,可以从数据JS文件中渲染图像和链接。我还想生成一个段落,但不确定可以在哪里调用段落标签以在我的projectlist.js和project.js组件上生成它。 我希望它在图像之下。

project.js

import "./project.css";

const Project = ({ img, link }) => {
  return (
    <div className="p">
      <div className="p-browser">
        <div className="p-circle">Tick</div>
        <div className="p-circle"></div>
        <div className="p-circle"></div>
      </div>
      <a href={link} target="_blank">
        <img src={img} alt="" className="p-img" />
        <p>kscjksnkdjs</p>
      </a>
      <div className="p-right">
        <div className="p-bg"></div>
      </div>
    </div>
  );
};

export default Project;

projectlist.js

import "./projectList.css";
import Project from "../project/Project";
import { projects } from "../../data";

const ProjectList = () => {
  //   console.log(projects);
  return (
    <div className="pl">
      <div className="pl-texts">
        <h1 className="pl-title">Projects</h1>
        <p className="pl-description">
          {/* Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsum sunt ad
          vitae. Quo maxime quos odit? Fuga omnis suscipit numquam adipisci unde
          aliquid vel deleniti ex eos excepturi. Atque, nostrum. */}
        </p>
      </div>
      <div className="pl-list">
        {projects.map((project) => (
          <Project key={project.id} img={project.img} link={project.link} />
        ))}
      </div>
      <div className="text">thsisihsjihih</div>
      <div className="pl-right">
        <div className="pl-bg"></div>
      </div>
    </div>
  );
};

export default ProjectList;

I have two component where I am rendering images and links from a data js file. I would also like to generate a paragraph but am unsure where I can call the paragraph tag to generate it on both my ProjectList.js and Project.js components.
I'd like it to be just under the image.

Project.js

import "./project.css";

const Project = ({ img, link }) => {
  return (
    <div className="p">
      <div className="p-browser">
        <div className="p-circle">Tick</div>
        <div className="p-circle"></div>
        <div className="p-circle"></div>
      </div>
      <a href={link} target="_blank">
        <img src={img} alt="" className="p-img" />
        <p>kscjksnkdjs</p>
      </a>
      <div className="p-right">
        <div className="p-bg"></div>
      </div>
    </div>
  );
};

export default Project;

ProjectList.js

import "./projectList.css";
import Project from "../project/Project";
import { projects } from "../../data";

const ProjectList = () => {
  //   console.log(projects);
  return (
    <div className="pl">
      <div className="pl-texts">
        <h1 className="pl-title">Projects</h1>
        <p className="pl-description">
          {/* Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsum sunt ad
          vitae. Quo maxime quos odit? Fuga omnis suscipit numquam adipisci unde
          aliquid vel deleniti ex eos excepturi. Atque, nostrum. */}
        </p>
      </div>
      <div className="pl-list">
        {projects.map((project) => (
          <Project key={project.id} img={project.img} link={project.link} />
        ))}
      </div>
      <div className="text">thsisihsjihih</div>
      <div className="pl-right">
        <div className="pl-bg"></div>
      </div>
    </div>
  );
};

export default ProjectList;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

傲鸠 2025-01-27 11:22:38

按照您上面的评论。

在其他情况下,要在 p 标记中显示每个项目的文本

您应该:

  1. 使 Project 组件接受另一个名为 text 类似于 imglink
  2. 使用插值 ({}) 将其显示在 Project 中的 p 中 然后将每个项目
  3. text 传递到map 中的 Project 组件

查看下面的代码示例

// Project component
const Project = ({ img, link, text }) => {
  return (
    <div className="p">

      ...rest of the code block
    
      <a href={link} target="_blank">
        <img src={img} alt="" className="p-img" />
        <p>{text}</p>
      </a>

      ...rest of the code block

    </div>
  );
};
// ProjectList
...rest of the code block

{projects.map((project) => ( 
  <Project key={project.id} img={project.img} link={project.link} text={project.text}/>
))}

...rest of the code block

另外,如果您希望在 p 中显示项目的文本> 标记在您的 ProjectList 组件中,那么您必须从项目列表中选择一个项目。

您可以使用零索引选择并显示列表中的第一个项目,如下所示

// ProjectList
const ProjectList = () => {
  //   console.log(projects);
  return (
    <div className="pl">
      <div className="pl-texts">

       ...rest of the code block

        <p className="pl-description">
          {projects[0].text}
        </p>

       ...rest of the code block

      </div>
    </div>
  );
};

Following your comments above.

In other to display each project's text in a p tag

You should:

  1. Make the Project component accept another prop value called text similar to img and link
  2. Use interpolation ({}) to display it within the p in the Project component
  3. Then pass each project's text into the Project component in the map

Checkout the code example below

// Project component
const Project = ({ img, link, text }) => {
  return (
    <div className="p">

      ...rest of the code block
    
      <a href={link} target="_blank">
        <img src={img} alt="" className="p-img" />
        <p>{text}</p>
      </a>

      ...rest of the code block

    </div>
  );
};
// ProjectList
...rest of the code block

{projects.map((project) => ( 
  <Project key={project.id} img={project.img} link={project.link} text={project.text}/>
))}

...rest of the code block

Also, if you wish to display a project's text in the p tag within your ProjectList component then you must pick one project from the list of projects.

You can pick and display the first project in the list using the zero index like this

// ProjectList
const ProjectList = () => {
  //   console.log(projects);
  return (
    <div className="pl">
      <div className="pl-texts">

       ...rest of the code block

        <p className="pl-description">
          {projects[0].text}
        </p>

       ...rest of the code block

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