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

失落9123193

我目前正在使用react来渲染一个名为prop的道具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

但是,当我包含时{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上,这两个大洲都出现了,但是“这是一个没有出现的国家”,但是我看到了

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都是一个对象,因此,你要渲染的对象为您的孩子<div>在你的renderCountries功能。如果只想显示国家/地区的名称,则应执行以下操作:

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

然后,您将看到一些有意义的输出。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React:Map 函数中的 Child Prop

如何优化React + Redux中嵌套组件的prop的小更新?

Python使用map()遍历列表中的列表

使用map在React中循环遍历数组时返回null而不是JSX

Flatlist遍历React Native中的嵌套数组

遍历JavaScript / React中的嵌套数组并出错

如何循环遍历 React 中的嵌套 JSON 数据?

如何在react js中循环遍历json嵌套对象

遍历嵌套在嵌套在数组 React 中的对象中的数组

如何使用nodejs遍历JSON中的嵌套对象?

如何使用for循环遍历Python中的嵌套列表

遍历嵌套的json对象并使用python获取表中的值

无法在嵌套在克隆对象中的数组上使用 map() 方法(获取 API)-React Hooks

在React中,当使用map()遍历元素数组时,是否仅需要使用<ul>和<li>标签显示结果?

React.js使用map遍历对象键和值

从 React 到 Vue,如何使用 .map 方法遍历数组?

无法使用 map() 方法在 React JS 上循环遍历状态

遍历HTML中的Map

遍历xsl中的嵌套节点

遍历python中的嵌套对象

遍历JPanel中的嵌套字段

遍历JavaScript中的嵌套对象

遍历NodeJS中的嵌套对象

遍历python中的嵌套元组

使用jQuery遍历嵌套对象

将mgo与转换为map [string] interface {}的嵌套文档一起使用。如何遍历地图?

ESLint:Typescript React中的'.map在props验证中丢失'(eslint react / prop-types)

如何使用map函数遍历reactjs中的对象列表?

如何为Typescript + React中的Object类型的可选嵌套prop提供默认值?