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

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

我目前正在使用 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"
        }]
    }]
}]

更新 2--

这个作品

 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 is the continent
 This is the country

结合一张地图,这个WORKS

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

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

这输出:

 Europe
 Asia

BUT 当我包含 {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>
    );
  }
}

在 Firefox 上,两个大陆都出现了,但 "this is a country doesn't show up" 而我得到的是

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.

好像是说 renderCountries 永远不能 运行。我对此仍然有点困惑,但我想我会尝试分离组件,看看它是否能解决问题。

两件事:

1) 在你问题的第二个代码块中,你正在做 area.countries.maparea 对象上的键名为 Countries,而不是 countriesarea.countries 应该是未定义的。

2) area.Countries 是一个对象数组,就像您在问题中所说的那样。所以是的,你可以很好地映射它们。问题是每个 Country 都是一个对象,因此,您试图在 renderCountries 函数中将对象呈现为 <div> 的子对象。如果你只想显示国家的名字,你应该这样做:

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

然后你会看到一些有意义的输出。

你打错了,用area.Countries代替area.countries

此外,我认为您至少应该创建 3 个组件:AreaCountryCity。然后你可以像这样渲染数据(请注意我使用 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>);
 }
}