简体   繁体   English

如果项目已存在于数组中,则 React 挂钩禁用按钮

[英]React hooks disable button if item already exists in the array

I have a Cart component where I save products in an array.我有一个 Cart 组件,我将产品保存在一个数组中。

I have also multiples buttons with differents id's that I would to disable independently, depending of each id when I add the product to the array.我还有多个带有不同 id 的按钮,当我将产品添加到数组时,我会根据每个 id 独立禁用这些按钮。

once I remove the item from the array I would like to turn back the button to able again.一旦我从数组中删除了该项目,我想重新打开按钮以再次启用。

I tried a couple of solutions but they would or disable all my buttons or once I could disable the add to cart I couldn't turn the button to able again on delete.我尝试了几种解决方案,但他们会或禁用我的所有按钮,或者一旦我可以禁用添加到购物车,我就无法在删除时再次打开按钮。 please guys I've run out of ideas!请伙计们我已经没有想法了!

// array to save elements in the cart
const [cart, setCart] = useState([])

return (
  <>
// array with all buttons
    {list.map((item) => {
      // function to add elements to the cart
      const addToCart = () => {
        const palestra = {
          horario_inicio: item.horario_inicio,
          horario_fim: item.horario_fim,
          id_programacao: item.programacao,
          nome_da_palestra: item.nome_da_palestra,
          palestrante: item.palestrante,
          imagem: item.imagem_palestra.url,
          id: item.id
        }
        setCart(curr => [...curr, palestra])
      }
      // function to delete items fro cart
      const handleRemoveItem = (itemId: string) => {
        setCart(cart.filter(({ id }) => id !== itemId));
      }

      return (
        <Box m="2" h="30%" pt={2} pb={2} border="1px solid #1C1C1C" backgroundSize="cover" borderRadius="20px" backgroundPosition="center" backgroundImage={`url(${(item.imagem_palestra?.url)})`} backgroundColor="white" align="center">
          <Center ml="5" mt="2">
            <Spacer />
            <>
      // I want to disable this button if his id exists in the array of
              // cart and anable in case I case i delete this item;
              <IconButton
                variant="ghost"
                mr="3"
                colorScheme="gray"
                borderRadius='md'
                aria-label="Save event"
                icon={<Icon as={BsBookmark} />}
                onClick={addToCart}
              />
              <IconButton
                variant="ghost"
                mr="3"
                colorScheme="gray"
                borderRadius='md'
                aria-label="Save event"
                icon={<Icon as={BsFillBookmarkFill} />}
                onClick={() => handleRemoveItem(item.id)}
              />
            </>
          </Center>
        </Box>
      )
    })}
  </>
                 

again I'm trying to disable only the button witch id matches with the id of the product in the cart array.我再次尝试仅禁用与购物车数组中产品的 ID 匹配的按钮女巫 ID。

Thank you in advance guys :)提前谢谢你们:)

How about using Array.prototype.some ?使用Array.prototype.some怎么样?

<IconButton
  ...
  disabled={cart.some(cartItem => cartItem.id === item.id)}
/>   

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

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