猿问

在 React 中显示数组中的对象 [i]

我想遍历一个对象数组并在组件中显示一个项目,其中 prop 是数组项目的索引号。


这是我的代码:


import React from 'react';

import './projecten.scss';

import Layout from './../Layout/Layout';

import { ProjectenLijst } from '../../../data';


const Projecten = () => {

  const ProjectItem = ({i}) => (

    <div>{ProjectenLijst[i]}</div>

  )


  return (

    <Layout>

      <ProjectItem i={2}/> // here I want to set the index number of the array

    </Layout>

  )

}


export default Projecten;

这是我从中提取数据的数据文件:


export const ProjectenLijst = [

  {

    naam: 'Project 1',

    id: 1,

    wat: 'website',

    url: 'https://www.project1.com',

    platform: 'WordPress',

    omschrijving: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis blandit magna leo, sit amet bibendum risus tincidunt non. Duis vitae ligula vel felis tincidunt facilisis. Maecenas interdum ligula ut vestibulum scelerisque. Aenean vestibulum ultrices augue. Mauris nec aliquam nulla, quis ultrices arcu. Aliquam fringilla.',

    img: ''

  },


  {

    naam: 'Project 2',

    id: 2,

    wat: 'website',

    url: 'https://www.project2.com',

    platform: 'WordPress',

    omschrijving: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis blandit magna leo, sit amet bibendum risus tincidunt non. Duis vitae ligula vel felis tincidunt facilisis. Maecenas interdum ligula ut vestibulum scelerisque. Aenean vestibulum ultrices augue. Mauris nec aliquam nulla, quis ultrices arcu. Aliquam fringilla.',

    img: ''

  },


我无法让它工作。代码呈现页面,但项目项应呈现的部分保持为空。我忽略了一些东西……但我做错了什么?:-)


qq_遁去的一_1
浏览 149回答 3
3回答

慕村225694

有两个问题。当您调用ProjectenLijstusing时,您必须使用大括号<ProjectItem i={2} />提取属性:iconst ProjectItem = (i) => (/* ... */);// should beconst ProjectItem = ({i}) => (/* ... */);请参阅解构赋值 - 对象解构您不能渲染复杂的对象,只能渲染标量值。意思是:<div>{ProjectenLijst[i]}</div>// should for example be<div>{ProjectenLijst[i].naam}</div>// mockconst Layout = (props) => <div {...props} />;const ProjectenLijst = [&nbsp; { naam: 'Project 1', id: 1 },&nbsp; { naam: 'Project 2', id: 2 },&nbsp; { naam: 'Project 3', id: 3 },];// answerconst Projecten = () => {&nbsp; const ProjectItem = ({i}) => (&nbsp; &nbsp; &nbsp; &nbsp; // extract the "i" property by using curly braces&nbsp; &nbsp; <div>{ProjectenLijst[i].naam}</div> // refer to a scalar property, not the whole object&nbsp; );&nbsp; return (&nbsp; &nbsp; <Layout>&nbsp; &nbsp; &nbsp; <ProjectItem i={2} />&nbsp; &nbsp; </Layout>&nbsp; );};ReactDOM.render(<Projecten />, document.querySelector("#root"));<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script><script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script><div id="root"></div>

斯蒂芬大帝

详细说明最初的评论,因为代码发生了变化:const ProjectItem = ({i}) => (&nbsp; <div>{ProjectenLijst[{i}]}</div>)首先,您制作了一个功能组件,因此您可以调用:<ProjectItem i={0} foo="foo" bar="bar">让我们重写功能组件以不破坏参数:const ProjectItem = (props)=> {&nbsp; console.log(props.i) //0&nbsp; console.log(props.foo) //"foo"&nbsp; console.log(props.bar) //"bar"&nbsp; ...}由于我们只需要iprop/key,我们可以解构它,包括参数:const ProjectItem = ({i})=>{...}或者在函数内部,假设我们想将整个 props 对象传递给另一个组件:const ProjectItem = (props)=>{&nbsp; const {i,foo} = props //to use locally&nbsp; const aThing = thingMaker(props)&nbsp; ...}让我们提醒自己如何索引数组:const i = 0const someObject = { i: i }const alternatively = { i } //shorthand to {i:i}const someArray = [0,1,2,3]console.log(someArray[ i ]) //0 first elementconsole.log(someArray[ someObject ]) //i think it will be undefined,&nbsp;简单地说,你不能用一个对象索引到数组中。将所有这些应用到您的原始代码段中,并删除大部分速记const ProjectItem = (props)=>{&nbsp; // get the index&nbsp; const { i } = props&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //destructure&nbsp; const { i: myIndex } = props //destructure and alias&nbsp; const _i = props.i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//same thing no destructuring&nbsp; // get the item (use any of these)&nbsp; const myItem0 = ProjectenLijst[i]&nbsp; const myItem1 = ProjectenLijst[props.i]&nbsp; const myItem2 = ProjectenLijst[myIndex]&nbsp; const myItem3 = ProjectenLijst[_i]&nbsp; //JSX&nbsp; return <div>&nbsp;&nbsp; &nbsp;{ myItem0 }&nbsp;&nbsp; </div>&nbsp;&nbsp;}请注意,{ myItem0 }您有一组大括号{},中间有一个变量。所以回顾一下,你在做什么:const ProjectItem = ({i}) => (&nbsp; <div>{ProjectenLijst[{i}]}</div>)是:const ProjectItem = (props)=> {&nbsp; const someArbitraryObject = { i: props.i }&nbsp; const item = ProjectenLijst[ someArbitraryObject ]&nbsp; console.log(someArbitraryObject) // { i: 0 }&nbsp; console.log(item) // undefined&nbsp; return <div>{item}</div>}这只是原始代码的问题之一 -索引到数组和解构。关于什么可以用 JSX 等呈现的其他答案也适用。

饮歌长啸

import React from 'react';import './projecten.scss';import Layout from './../Layout/Layout';import { ProjectenLijst } from '../../../data';const Projecten = () => {&nbsp; const ProjectItem = (i) => (&nbsp; &nbsp; <div>{ProjectenLijst[i].name}</div>&nbsp; )&nbsp; return (&nbsp; &nbsp; <Layout>&nbsp; &nbsp; &nbsp; {ProjectenLijst.map((item, index) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return <ProjectItem i={index}/>&nbsp; &nbsp; &nbsp; })}&nbsp; &nbsp; </Layout>&nbsp; )}export default Projecten;或者import React from 'react';import './projecten.scss';import Layout from './../Layout/Layout';import { ProjectenLijst } from '../../../data';const Projecten = () => {&nbsp; const ProjectItem = (item) => (&nbsp; &nbsp; <div>{item.name}</div>&nbsp; )&nbsp; return (&nbsp; &nbsp; <Layout>&nbsp; &nbsp; &nbsp; {ProjectenLijst.map(item => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return <ProjectItem item={item}/>&nbsp; &nbsp; &nbsp; })}&nbsp; &nbsp; </Layout>&nbsp; )}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答