简体   繁体   English

使用map()遍历React中的嵌套Prop

[英]Using map() to iterate through a nested Prop in React

I'm currently using react to render a prop called area which looks like this: 我目前正在使用react渲染一个名为area的道具,如下所示:

[{
    "id": 1,
    "name": "Europe",
    "Countries": [{
        "id": 1,
        "name": "Iceland",
        "Cities": [{
            "id": 1,
            "name": "Selfoss"
        }]
    }, {
        "id": 2,
        "name": "Switzerland",
        "Cities": [{
            "id": 2,
            "name": "Geneva"
        }]
    }]
}, {
    "id": 2,
    "name": "Asia",
    "Countries": [{
        "id": 3,
        "name": "Japan",
        "cities": [{
            "id": 3,
            "name": "Yokohama"
        }]
    }]
}]

UPDATE 2-- 更新2-

This WORKS : 工作

 class AreaBar extends Component {
  constructor(props) {
    super(props);
   }

  .....

  renderCountries() {
    return(
      <div>
        This is the country
      </div>
      )
    }

  renderContinents() {
    return(
      <div>
        This is the continent
        {this.renderCountries()}
      </div>
      )
    }

  render() {
    return(
        <div> 
            {this.renderContinents()}
        </div>
    );
  }
}

This outputs: 输出:

 This is the continent
 This is the country

Incorporating a map, this WORKS 结合地图,这个工程

  renderContinents(area) {
    return(
      <div>
        {area.name}
      </div>
      )
    }

  render() {
    return(
        <div> 
            {this.props.areas.map(this.renderContinents)}
        </div>
    );
  }
}

This outputs: 输出:

 Europe
 Asia

BUT when I include {this.renderCountries()} , it doesn't output anything, which I think is why I couldn't get the suggestions to work. 但是当我包含{this.renderCountries()} ,它什么也不会输出,这就是为什么我无法获得建议的原因。

  renderCountries() {
    return(
      <div>
        This is the country          
      </div>
      )
    }


  renderContinents(area) {
    return(
      <div>
        {area.name}
        {this.renderCountries()}
      </div>
      )
    }


  render() {
    return(
        <div> 
            {this.props.areas.map(this.renderContinents)}
        </div>
    );
  }
}

On Firefox, both of the continents show up but "this is a country doesn't show up" instead I get a 在Firefox上,两个大陆都出现了,但是“这是一个没有出现的国家”,相反,

unreachable code after return statement

When an expression exists after a valid return statement, 
a warning is given to indicate that the code after the return 
statement is unreachable, meaning it can never be run.

It seems like it's saying renderCountries can never be run. 似乎在说renderCountries永远无法运行。 I'm still a bit confused about this but I think I'm going to try to separate the components and see if it fixes the issue. 我对此仍然有些困惑,但是我想我将尝试分离这些组件,并查看它是否可以解决问题。

Two things: 两件事情:

1) In the second block of code in your question, you're doing area.countries.map . 1)在问题的第二段代码中,您正在执行area.countries.map The key on your area object is called Countries , not countries . area对象上的键称为“ Countries ,而不是“ countries area.countries should be undefined. area.countries应该未定义。

2) area.Countries is an array of objects, like you said in your question. 2) area.Countries是一个对象数组,就像您在问题中所说的那样。 So yes, you can map over them just fine. 所以是的,您可以在它们上面映射。 The problem is that each Country is an object, and thus, you're trying to render an object as a child of your <div> in your renderCountries function. 问题在于每个Country都是一个对象,因此,您试图在renderCountries函数中将对象作为<div>的子级进行渲染。 If you only want to display the Country's name, you should do something like this: 如果只想显示国家/地区的名称,则应执行以下操作:

renderCountries(country){
  return(
    <div>
    {country.name}
    </div>
  )
}

Then you will see some meaningful output. 然后,您将看到一些有意义的输出。

you have a typo, use area.Countries instead of area.countries 您有错别字,请使用area.Countries而不是area.countries

Also I think you should create 3 components at least: Area , Country and City . 另外,我认为您至少应创建3个组件: AreaCountryCity Then you can render the data like so (please note I use ES6 syntax) : 然后,您可以像这样渲染数据(请注意,我使用ES6语法):

var areas = [/* array you posted above*/];

// in your top-level component
render() {
 return (<div>
  {areas.map((area) => {
   return <Area data={area}/>;
  })}
 </div>);
}


// Area component
export function Area({data}) {
 render() {
  return (<div>
   Area name: {data.name}
   {data.Countries.map((country) => {
    return <Country data={country}/>
   })}
  </div>);
 }
}

// Country component
export function Country({data}) {
 render() {
  return (<div>
   Country: {data.name}
   {data.Cities.map((city) => {
    return <City data={city}/>
   })}
  </div>);
 }
}

// City component
export function City({data}) {
 render() {
  return (<div>
   City: {data.name}
  </div>);
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM