简体   繁体   English

如何使用 onClick 事件禁用反应组件(或 onClick 本身)

[英]How to disable a react component (or onClick itself) with onClick event

I have seen similar questions but none of the answers are working in my case and I hope someone will be able to tell me why.我见过类似的问题,但在我的情况下没有一个答案有效,我希望有人能够告诉我原因。

My ReactApp renders 3 card components that flip when clicked on.我的 ReactApp 呈现了 3 个卡片组件,点击时它们会翻转。 The cards are populated with data from an object array and is rendered with a map function (added info in case it has an impact).这些卡填充了来自 object 阵列的数据,并使用 map function 进行渲染(添加信息以防产生影响)。 Here is parent component.这是父组件。

import React from 'react'
import FlipCard from './FlipCard'

const cards = [
    {
        id: 1,
        text: 'NOPE',
    },
    {
        id: 2,
        text: '!!WINNER!!',
    },
    {
        id: 3,
        text: 'NOPE',
    },
]

const shuffleCards = array => {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1))
        const temp = array[i]
        array[i] = array[j]
        array[j] = temp
    }
    return array
}
shuffleCards(cards)

console.log(cards)

const CardGameUI = () => {
    shuffleCards(cards)

    return (
        <div className="cards-ui">
            {cards.map(card => (
                <FlipCard key={card.id} text={card.text} value={card.id} />
            ))}
        </div>
    )
}

export default CardGameUI

When one of the cards are flipped, I need the onClick for the other cards to be disabled.当其中一张卡被翻转时,我需要 onClick 来禁用其他卡。 I tried using state and a conditional in my onClick event but it has no effect.我尝试在我的 onClick 事件中使用 state 和条件,但它没有效果。 The according to the console.log, the state of the play boolean is changed and if I manually change the conditional in the onCLick event check if play is true, then it works perfectly fine.根据console.log,播放boolean的state已更改,如果我手动更改onCLick事件中的条件,检查播放是否为真,然后它工作得很好。 I am clearly missing something because it seems as though the conditional is working and the state is changing.我显然遗漏了一些东西,因为似乎条件正在工作并且 state 正在改变。

import React, { useState } from 'react'
import ReactCardFlip from 'react-card-flip'
import FrontComponent from './FrontComponent'
import BackComponent from './BackComponent'

const FlipCard = ({ text, value }) => {
    const [isFlipped, setIsFlipped] = useState(false)
    const [activeCard, setActiveCard] = useState(2)
    const [play, setPlay] = useState(false)
    console.log(play.valueOf())

    function handleFlip() {
        setPlay(true)
        setIsFlipped(!isFlipped)
        console.log(isFlipped)
        setActiveCard(value)
        console.log(value)
    }

    if (activeCard !== 2) {
        console.log('Play again?')
    }

    return (
        <>
            <ReactCardFlip isFlipped={isFlipped} flipDirection="horizontal">
                <FrontComponent onClick={!play ? handleFlip : null} />

                <BackComponent text={text} value={value} />
            </ReactCardFlip>
        </>
    )
}

export default FlipCard

What am I missing?我错过了什么?

You should manage the onClick event and the flip state on the parent instead of inside the card.您应该在父级而不是卡内管理onClick事件和flip state。

An high-level overview will be:高级概述将是:

const CardGameUI = () => {
  const [flipped, setFlipped] = useState({});
  const hasFlipped = Object.values(flipped).includes(true);

  const handleFlip = id => () => setFlipped(flipped => ({
    ...flipped,
    [id]: true // can be changed to toggle in case you need it in the future
  }));

  return (
    <div>
      {cards.map(({ id, text }) => (
        <FlipCard
          key={id}
          text={text}
          value={id}
          onFlip={handleFlip(id)}
          flipped={flipped[id]}
          disabled={hasFlipped}
        />
      ))}
    </div>
  )
};

const FlipCard = ({ text, value, onFlip, flipped , disabled}) => {
  return (
    <ReactCardFlip isFlipped={flipped} flipDirection="horizontal">
      <FrontComponent onClick={onFlip} disabled={disabled} />
      <BackComponent text={text} value={value} />
    </ReactCardFlip>
  )
}

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

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