简体   繁体   English

错误警告:列表中的每个孩子都应该有一个唯一的“关键”道具

[英]ERROR Warning: Each child in a list should have a unique "key" prop

Why is it showing me this error?为什么它向我显示此错误?

I have an app created with npx react-native init myProyect created in my React Native studios.我在我的 React Native 工作室中创建了一个使用 npx react-native init myProyect 创建的应用程序。

I'm testing the way to add the styles with 'styled-components / native'.我正在测试使用“styled-components / native”添加 styles 的方法。

I want to show different companies, with their name, address, icon of the type of company that it is, image of the company and rating stars.我想展示不同的公司,包括他们的名称、地址、公司类型的图标、公司形象和评级星级。

I have added an svg image for the stars.我为星星添加了 svg 图片。 For the company image, for testing purposes, I have added it by URL, but I want to add the image from the assets folder, and I don't know how to do it.公司图片,为了测试,我已经通过URL添加了,但是我想从assets文件夹中添加图片,不知道怎么办。

Until now you could see the company image, the stars and the CLOSE image, which is also svg and I bring it from a file.到目前为止,您可以看到公司图像、星星和关闭图像,它也是 svg,我从文件中获取它。 When I have tried to add the icon to show the category of the company it is, the icon is not displayed, and despite running the App, the console shows me the following error:当我尝试添加图标以显示公司类别时,图标未显示,尽管运行了应用程序,但控制台显示以下错误:

LOG Running "searchYourBusiness" with {"rootTag": 121}
 ERROR Warning: Each child in a list should have a unique "key" prop.

Check the render method of `StoreInfo`. See https://reactjs.org/link/warning-keys for more information.
SvgXml @ http: //10.0.2.2: 8081 / index.bundle? Platform = android & dev = true & minify = false & app = com.searchYourBusiness & modulesOnly = false & runModule = true: 140774: 31
    in StoreInfo (at StorePantalla.js: 31)
    in RCTView (at View.js: 32)
    in View
    in StyledNativeComponent (created by Styled (View))
    in Styled (View) (at StorePantalla.js: 30)
    in RCTView (at View.js: 32)
    in View (at SafeAreaView.js: 41)
    in SafeAreaView
    in StyledNativeComponent (created by Styled (Component))
    in Styled (Component) (at StorePantalla.js: 23)
    in StoreDisplay (at App.js: 35)
    in ThemeProvider (at App.js: 34)
    in App (at renderApplication.js: 48)
    in RCTView (at View.js: 32)
    in View (at AppContainer.js: 106)
    in RCTView (at View.js: 32)
    in View (at AppContainer.js: 133)
    in AppContainer (at renderApplication.js: 41)
    in searchYourBusiness (RootComponent) (at renderApplication.js: 57)

I have looked for solution in other similar answers on this site, but I have not managed to solve:我在这个网站上的其他类似答案中寻找解决方案,但我没有设法解决:

Warning: Each child in a list should have a unique "key" prop 警告:列表中的每个孩子都应该有一个独特的“关键”道具

Warning: Each child in an array or iterator should have a unique "key" prop. 警告:数组或迭代器中的每个孩子都应该有一个唯一的“键”道具。 Check the render method of `ListView` 检查 `ListView` 的渲染方法

React Warning: Each child in a list should have a unique "key" prop. React 警告:列表中的每个孩子都应该有一个独特的“关键”道具。 in render() function 在渲染()function

How to fix Warning: Each child in a list should have a unique "key" prop 如何修复警告:列表中的每个子项都应该有一个唯一的“键”道具

Not what else to do to correct the error and display the icon.更正错误并显示图标。

How can I show the icon and eliminate the error?如何显示图标并消除错误?

What should I do to show an icon and an image from my assets folder according to my structured code?根据我的结构化代码,我应该怎么做才能显示资产文件夹中的图标和图像?

I show the files involved in this我展示一下这里面涉及的文件

file App.js文件 App.js

import React from 'react'
import { StyleSheet, View, Text } from 'react-native'
import { ThemeProvider } from 'styled-components/native'

import { theme } from './src/theme'

import StorePantalla from './src/features/stores/pantallaStore/StorePantalla'


export default function App() {

  return (
    <ThemeProvider theme={theme}>
      <StorePantalla />
    </ThemeProvider>
  )
  }

const styles = StyleSheet.create({
})

File StoreInfo.js文件 StoreInfo.js

import React from 'react'
import { View, Text, StyleSheet, Image } from 'react-native'
import { Card } from 'react-native-paper'
import styled from 'styled-components/native'
import { SvgXml } from 'react-native-svg'
import star from '../../../../assets/star'
import closed from '../../../../assets/closed'

const StoreCard = styled(Card)`
  background-color: ${(props) => props.theme.colors.bg.secondary}`

const StoreCardCover = styled(Card.Cover)`
  padding: ${(props) => props.theme.space[4]}
  background-color: ${(props) => props.theme.colors.bg.primary}
  `

const Title = styled.Text`
  font-family: ${(props) => props.theme.fonts.heading}
  padding-left: ${(props) => props.theme.space[3]}
  padding-bottom: ${(props) => props.theme.space[1]}
  fontSize: ${(props) => props.theme.sizes[2]}
  color: ${(props) => props.theme.colors.text.primary}
`

const Address = styled(Text)`
  font-family: ${(props) => props.theme.fonts.body}
  padding-left: ${(props) => props.theme.space[3]}
  padding-bottom: ${(props) => props.theme.space[4]}
`

const Info = styled(View)`
  padding: ${(props) => props.theme.space[2]}
`

const Rating = styled(View)`
  flex-direction: row;
  padding-left: ${(props) => props.theme.space[2]}
  padding-bottom: ${(props) => props.theme.space[2]}
`

const Section = styled(View)`
  flex-direction: row;
  align-items: center;
`
const SectionEnd = styled(View)`
  flex: 1;
  flex-direction: row;
  justify-content: flex-end;
`
const Icon = styled(Image)`
  width= 35px;
  height= 35px;
  margin-left: ${(props) => props.theme.space[3]}
`

export const StoreInfo = ({ store = {} }) => {
  const {
    name = "Online Company",
    //image= require('../../../../assets/logos.jpg'),
    //photos = ["https://cms-assets.tutsplus.com/cdn-cgi/image/width=360/uploads/users/1125/posts/30546/preview_image/RN.jpg"],

    icon = "https://img.icons8.com/material-two-tone/384/000000/espresso-cup--v2.png",
    photos = ["https://cdn.pixabay.com/photo/2015/09/09/19/56/office-932926_1280.jpg"],
    address = "Charcos Enbarrados, 6 Ninguna Parte 04593",
    rating = 4,
    isClosed = true,
  } = store

  const ratingArray = Array.from(new Array(Math.floor(rating)))

  return (
    <StoreCard
      elevation={5}
    >
      <StoreCardCover
        key={name}
        resizeMethod='scale'
        source={{ uri: photos[0] }}
      />
      <Info>
        <Title> {name} </Title>
        <Section>
          <Rating
           style={styles.rating}
          >
            {ratingArray.map(() => (
              <SvgXml xml={star} width={30} height={30} />
            ))}
          </Rating>
          <SectionEnd>
            {isClosed && <SvgXml xml={closed} width={40} height={40} />}
            <Icon
              source={{ uri: icon }} />
          </SectionEnd>
        </Section>
        <Address> {address} </Address>
      </Info>
    </StoreCard>
  )
}

 const styles = StyleSheet.create({
  rating: {
    paddingLeft: 20
  }
})  

File StorePantalla.js文件存储Pantalla.js

import React from 'react'
import { View, SafeAreaView } from 'react-native'
import { Searchbar } from 'react-native-paper'
import { StoreInfo } from '../componentStore/StoreInfo'
import styled  from 'styled-components/native'

const SafeArea = styled(SafeAreaView)`
  flex:1;
`
const BarSearch = styled(View)`
  padding: ${(props) => props.theme.space[3]}
`
const StoreList = styled(View)`
    flex: 1;
    background-color: #00BCD4;
    padding: 1${(props) => props.theme.space[2]}
`


export default function StorePantalla() {

  return (
    <SafeArea>
      <BarSearch>
        <Searchbar
          placeholder="Search"

        />
      </BarSearch>
      <StoreList>
        <StoreInfo/>
      </StoreList>
    </SafeArea>
  )
}

The issue is here:问题在这里:

{ratingArray.map(() => (
          <SvgXml xml={star} width={30} height={30} />
        ))}

you have to add a key property, something like:您必须添加一个关键属性,例如:

{ratingArray.map((_, i) => (
          <SvgXml key={i} xml={star} width={30} height={30} />
        ))}

For more info look at the docs: https://reactjs.org/docs/lists-and-keys.html#basic-list-component有关更多信息,请查看文档: https://reactjs.org/docs/lists-and-keys.html#basic-list-component

暂无
暂无

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

相关问题 像这样的警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - warning like : Each child in a list should have a unique "key" prop 反应警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - React Warning: Each child in a list should have a unique "key" prop 警告:列表中的每个孩子都应该有一个唯一的“key”道具 - Warning: Each child in a list should have a unique “key” prop 错误:警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - ERR : Warning: Each child in a list should have a unique "key" prop 修复警告 ///列表中的每个孩子都应该有一个唯一的“键”道具 - fixing warning ///Each child in a list should have a unique "key" prop Reactjs:警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Reactjs: Warning Each child in a list should have a unique "key" prop 警告:列表中的每个孩子都应该有一个唯一的“键”道具来切换 - Warning: Each child in a list should have a unique "key" prop for switch 警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Warning Each child in a list should have a unique "key" prop 显示 API 数据时出错。 警告:列表中的每个孩子都应该有一个唯一的“key”道具 - Error when show API data. Warning: Each child in a list should have a unique "key" prop ES6 地图错误:“警告:列表中的每个孩子都应该有一个唯一的“键”道具” - ES6 Map Error: "Warning: Each child in a list should have a unique "key" prop"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM