首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >React-Native:数组中的每个子级都应该有唯一的键,即使我包含了键

React-Native:数组中的每个子级都应该有唯一的键,即使我包含了键
EN

Stack Overflow用户
提问于 2017-11-05 18:30:44
回答 1查看 245关注 0票数 1

我刚开始使用React-Native,在处理array.As的map时遇到了一个问题,所有元素都需要一个唯一的键。我已经在render方法中指定了键,但我仍然收到警告。以下是代码

代码语言:javascript
运行
复制
import React, { Component } from 'react';
import {Text , ScrollView} from 'react-native';
import axios from 'axios';
import AlbumDetail from './AlbumDetail'
export default class AlbumList extends Component {
  state = {albums: ['Fetching Data...']};
  componentWillMount() {
    console.log('fetching data');
    // Make a request
    axios.get('http://rallycoding.herokuapp.com/api/music_albums')
      .then(response => this.setState({albums: response.data}));
  }
  renderAlbums() {
    return this.state.albums.map(album =>
      <AlbumDetail
        key = {album.title}
        album = {album}
      />
    );
  }
  render() {
    return(
      <ScrollView>
        {this.renderAlbums()}
      </ScrollView>
    );
  }
}

这是我收到的警告,即使在包含了密钥之后

代码语言:javascript
运行
复制
ExceptionsManager.js:73 Warning: Each child in an array or iterator should have a unique "key" prop.

Check the render method of `AlbumList`. See https://fb. me/react-warning-keys for more information.
    in AlbumDetail (at AlbumList.js:16)
    in AlbumList (at index.js:21)
    in RCTView (at View.js:113)
    in View (at index.js:19)
    in FirstApp (at renderApplication.js:35)
    in RCTView (at View.js:113)
    in View (at AppContainer.js:102)
    in RCTView (at View.js:113)
    in View (at AppContainer.js:122)
    in AppContainer (at renderApplication.js:34)
EN

回答 1

Stack Overflow用户

发布于 2017-11-06 07:37:10

看看你的错误。它说错误是在AlbumList的render调用中。您指定的代码没有问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47120517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档