Reactの学習を始めたばかりですが、同じコンポーネントで2つの異なる配列をマッピングおよびレンダリングする方法を理解するのに問題があります。私のCMSには、タイトルと説明をそれぞれ含む3つのエントリがあります。私はそれらをGraphQLからvarで呼び出していますdata
。コンポーネントにSectionLeft
は、threeFeatures
classNameとしてのグリッドコンテナがあります。私のホームページでは、小道具を使ってコンテンツをグリッドコンテナに渡していますbottomcontent
。SingleFeature
グリッド列であり、アイコン、タイトル、説明を含む配列でコンポーネントを渡します。これは3回繰り返され、3列x1行のグリッドを構成します。コンポーネントにSingleFeature
はchildren
、定数の配列マップを含む必要がある小道具がありますiconList
3つの異なるsvgアイコンコンポーネントが含まれています。以下に表示されてSingleFeature
いるのと同じdata
配列を小道具feature
とdetails
に表示するコンポーネントの複数配列マップを作成する方法を理解できませんが、3つのコンポーネントをchildren
配列内にレンダリングします。どんな説明でも大歓迎です。ありがとうございました。
ホームページ.js
import IconOne from "../components/icons/icon-one"
import IconTwo from "../components/icons/icon-two"
import IconThree from "../components/icons/icon-three"
export const iconList = [IconOne, IconTwo, IconThree]
export default function Home({data}) {
return (
<div>
<SectionLeft bottomcontent =
{data.allContentfulHomepageWordpressFeatures.edges.map(edge => (
<SingleFeature
children = {
...iconList array...
}
feature = {
edge.node.feature
}
details = {
edge.node.description
}
/>
))
}
/>
);
}
section-left.js
export default function SectionLeft(props) {
return (
<div className="threeFeatures">{props.bottomcontent}</div>
);
}
single-feature.js
export default function SingleFeature(props) {
return(
<div>
{props.children}
<h3>{props.feature}</h3>
<p>{props.details}</p>
</div>
);
}
GraphQLクエリ
export const query = graphql`
allContentfulHomepageSpeedFeatures {
edges {
node {
feature
description
}
}
}
}
`
私はそれをこのようにします:
data.allContentfulHomepageWordpressFeatures.edges.map((edge, index) => (
<SingleFeature
children = {
iconList[index]
}
feature = {
edge.node.feature
}
details = {
edge.node.description
}
/>
))
mapは、マップされた配列内の要素のインデックスを2番目のパラメーターとしてarrow関数に渡します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加