Я пытаюсь сделать компонент в React более динамичным, сохраняя заголовки в массиве и каждый раз сопоставляя их с элементом html. Однако с каждой картой элемента он выводит все значения в массиве заголовков. Я бы хотел, чтобы первая итерация имела первое значение заголовка, вторая — второй заголовок и так далее. Для тех, кто этого не знает, беспорядочный CSS — это Tailwind.
** Это было решено **
Однако, следуя дальше, я также хотел бы, чтобы текст Lorem отображался из массива так же, как заголовки.
Вот компонент:
import * as React from 'react';
//Maps element based on the number of itterations
const itterations = [1, 2, 3, 4, 5, 6];
const headings = ['text', 'text2', 'text3', 'text4', 'text5', 'text6'];
const items = itterations.map((itterations) =>
<div class = "flex flex-row lg:flex-col mx-5 lg:mx-2 lg:mt-0 my-5 pb-5 items-center md:items-start lg:bg-white border-b-2 border-red-300">
<a href = "#" class = "w-[26%] lg:w-full mr-5 lg:mr-0 rounded-sm"><img src = "./images/elden-ring-thumbnail.jpg" alt = "Lorem Impsum"></img></a>
<div class = "md:flex md:flex-col lg:px-3">
<h6 class = "font-bold lg:my-3">{headings}</h6>
<p class = "hidden md:block my-2 lg:my-0">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sodales.</p>
</div>
</div>
);
export default function RecentReviews() {
return (
<div class = "mx-1 lg:w-[60%] lg:m-auto">
<h3 class = "bg-red-500 font-bold text-white my-5 lg:my-0 px-4 py-0.5">Recent Reviews</h3>
<div class = "lg:grid grid-cols-3 grid-row-2">
<>{items}</>
</div>
</div>
);
};
Хотя вы могли бы учитывать индекс при переборе itterations
, чтобы добраться до соответствующего элемента в headings
, переменная itterations
выглядит совершенно лишней, поскольку на самом деле она не используется. Карта вместо headings
.
const headings = ['text', 'text2', 'text3', 'text4', 'text5', 'text6'];
const items = headings.map((heading) =>
<div class = "flex flex-row lg:flex-col mx-5 lg:mx-2 lg:mt-0 my-5 pb-5 items-center md:items-start lg:bg-white border-b-2 border-red-300">
<a href = "#" class = "w-[26%] lg:w-full mr-5 lg:mr-0 rounded-sm"><img src = "./images/elden-ring-thumbnail.jpg" alt = "Lorem Impsum"></img></a>
<div class = "md:flex md:flex-col lg:px-3">
<h6 class = "font-bold lg:my-3">{heading}</h6>
<p class = "hidden md:block my-2 lg:my-0">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sodales.</p>
</div>
</div>
);
Я реализовал как заголовок, так и сообщение, создав массив объектов json, а затем сопоставив его.
import * as React from 'react';
//Maps element based on the number of itterations
const content = [{'heading': 'text1', 'text': 'text1'}, {'heading': 'text2', 'text': 'text2'}, {'heading': 'text3', 'text': 'text3'}, {'heading': 'text4', 'text': 'text4'}, {'heading': 'text5', 'text': 'text5'}, {'heading': 'text6', 'text': 'text6'},];
const items = content.map((content) =>
<div class = "flex flex-row lg:flex-col mx-5 lg:mx-2 lg:mt-0 my-5 pb-5 items-center md:items-start lg:bg-white border-b-2 border-red-300">
<a href = "#" class = "w-[26%] lg:w-full mr-5 lg:mr-0 rounded-sm"><img src = "./images/elden-ring-thumbnail.jpg" alt = "Lorem Impsum"></img></a>
<div class = "md:flex md:flex-col lg:px-3">
<h6 class = "font-bold lg:my-3">{content.heading}</h6>
<p class = "hidden md:block my-2 lg:my-0">{content.text}</p>
</div>
</div>
);
export default function RecentReviews() {
return (
<div class = "mx-1 lg:w-[60%] lg:m-auto">
<h3 class = "bg-red-500 font-bold text-white my-5 lg:my-0 px-4 py-0.5">Recent Reviews</h3>
<div class = "lg:grid grid-cols-3 grid-row-2">
<>{items}</>
</div>
</div>
);
};
Спасибо, что сработало. Я довольно новичок в React, поэтому это было полезное понимание лучших практик. Исходя из этого, где текст Lorem, я также хотел бы отобразить массив. Как бы это выглядело>