简体   繁体   English

删除一个总是返回数组最后一项的组件

[英]Delete a component always returning the last item of the array

I'm trying to delete a component receiving your id as param for my function deleteCard().我正在尝试删除一个组件,该组件接收您的 id 作为我的 function deleteCard() 的参数。 This component is wraped in an array, but at trigger the function this always deletes the last item of the array.该组件包装在一个数组中,但在触发 function 时,这始终会删除数组的最后一项。 I'm using map to render my array of cards.我正在使用 map 来渲染我的卡片阵列。

Please check the code in my GitHub.请检查我的 GitHub 中的代码。 I'm using react Modal for control the cards rendering and the deleteCard function is invoked from an external file:我正在使用 react Modal 来控制卡片渲染,并从外部文件调用 deleteCard function:

https://github.com/pablolucio97/places-to-know https://github.com/pablolucio97/places-to-know

Card compoenent:卡组件:

const Card = ({
    id,
    local,
    countryName,
    countryFlag,
    goalDate,
    openModalDelete,
    openModalEdit
} : countryCardTypes) => {
    return (
            <CardContainer key={id}>
                <TopContainer>
                    <CountryInfoContainer>
                        <ImageFlag src={countryFlag} />
                        <CountryTitle>{countryName}</CountryTitle>
                    </CountryInfoContainer>
                    <ButtonsContainer>
                        <EditButton onClick={openModalEdit}>
                            <MdEdit size={18} color='#333'/>
                        </EditButton>
                        <CloseButton onClick={openModalDelete}>
                            <MdClose size={18} color='#333'/>
                        </CloseButton>
                    </ButtonsContainer>
                </TopContainer>
                <Divider />
                <BottomContainer>
                    <Text>Local: {local}</Text>
                    <Text>Meta: {goalDate}</Text>
                </BottomContainer>
            </CardContainer>
    )
}

export default Card

Function to delete card: Function 删除卡:

  async function deleteCard(id: number){
    await axios.delete(`http://localhost:3333/cards/${id}`)
  }

Rendering the array of cards:渲染卡片数组:

  {
          countriesCards?.map(card => (
            <Card
              id={card.id}
              local={card.local}
              goalDate={card.goalDate}
              countryName={card.countryName}
              countryFlag={card.countryFlag}
              openModalEdit={() => { setShowModalEdit(true) }}
              openModalDelete={() => { setShowModalDelete(true) }}
            />
          ))
        }

What to do for my deleteCard() deletes the current card that trigger this function?我的 deleteCard() 删除触发此 function 的当前卡怎么办?

Please check the code in my GitHub.请检查我的 GitHub 中的代码。 I'm using react Modal for control the cards rendering and the deleteCard function is invoked from an external file:我正在使用 react Modal 来控制卡片渲染,并从外部文件调用 deleteCard function:

https://github.com/pablolucio97/places-to-know https://github.com/pablolucio97/places-to-know

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

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