简体   繁体   English

如何检测某个反应组件是否被点击

[英]How to detect if certain react component is clicked

I'm implementing a klondike game in which there are 4 empty stacks in the beiginning of the games.我正在实施一个 klondike 游戏,其中在游戏开始时有 4 个空堆栈。 The page looks like the first picture.该页面看起来像第一张图片。 The top left 4 blocks are empty.左上角的 4 个方块是空的。 I need the console to print out which component I clicked on.我需要控制台打印出我点击了哪个组件。 I tried to use the onclick event handler, but the "event.target" gives me nothing if I didn't click on a card, so When I clicked on those empty stack 1-4, I cannot know the result.我尝试使用 onclick 事件处理程序,但是如果我没有点击卡片,“event.target”什么也不会给我,所以当我点击那些空堆栈 1-4 时,我无法知道结果。 Can anyonee help me solve the problem here?任何人都可以帮我解决这里的问题吗? ( https://i.stack.imgur.com/9fApY.png ) ( https://i.stack.imgur.com/9fApY.png )

Here's my main frame.这是我的主框架。 Notice that Pile is another component and state.stack1-4 are state variables.请注意,Pile 是另一个组件,state.stack1-4 是 state 个变量。

return (
            <GameBase onClick={onClick}>
                <CardRow>
                    <Pile cards={state.stack1} spacing={0} onClick={onClick}/>
                    <Pile cards={state.stack2} spacing={0} onClick={onClick}/>
                    <Pile cards={state.stack3} spacing={0} onClick={onClick}/>
                    <Pile cards={state.stack4} spacing={0} onClick={onClick}/>
                    <CardRowGap/>
                    <Pile cards={state.draw} spacing={0} onClick={onClick}/>
                    <Pile cards={state.discard} spacing={0} onClick={onClick}/>
                </CardRow>
                <CardRow>
                    <Pile cards={state.pile1} onClick={onClick}/>
                    <Pile cards={state.pile2} onClick={onClick}/>
                    <Pile cards={state.pile3} onClick={onClick}/>
                    <Pile cards={state.pile4} onClick={onClick}/>
                    <Pile cards={state.pile5} onClick={onClick}/>
                    <Pile cards={state.pile6} onClick={onClick}/>
                    <Pile cards={state.pile7} onClick={onClick}/>
                </CardRow>
                <div id="errorMsg"></div>
            </GameBase>
)

For other places that contain cards, because those cards locate in the certain state variable arrays, I can use cards to retrieve the array name.For example, pile6 and 7 contain cards so I can use the card ID to know it's in pile 7/pile6 (Code below are inside the Onclick Handler"):对于其他包含卡片的地方,因为这些卡片位于某个 state 变量 arrays 中,我可以使用卡片来检索数组名称。例如,pile6 和 7 包含卡片,因此我可以根据卡片 ID 知道它在堆 7/ pile6(以下代码在 Onclick Handler 中):

    state.pile6.map(function (element, key) {
                if (`${element.suit}:${element.value}` === target.id) {
                    state.dst = "pile6";
                    state.targetIndex = key;
                    state.ValidArea = true;
                }
    });
    
    state.pile7.map(function (element, key) {
                if (`${element.suit}:${element.value}` === target.id) {
                    state.dst = "pile7";
                    state.targetIndex = key;
                    state.ValidArea = true;
                }
    });

But if I use similar code for stack, because it doesn't have cards, it will never be executed.但是如果我对stack使用类似的代码,因为它没有卡片,它永远不会被执行。

    state.stack1.map(function (element, key) {
       if (`${element.suit}:${element.value}` === target.id) {
             state.dst = "stack1";
             state.targetIndex = key;
             state.ValidArea = true;
        }
    });

The issue is you're trying to use the same onClick function for everything.问题是您正在尝试对所有内容使用相同的onClick function。 You should make separate functions and paramaterize them so you know what got clicked on.您应该制作单独的功能并将它们参数化,以便您知道点击了什么。

<GameBase onClick={handleGameBaseClick}>
      <CardRow>
          <Pile cards={state.stack1} spacing={0} onClick={()=>handleStackClick(1)}/>
          <Pile cards={state.stack2} spacing={0} onClick={()=>handleStackClick(2)}/>
          <Pile cards={state.stack3} spacing={0} onClick={()=>handleStackClick(3)}/>
          <Pile cards={state.stack4} spacing={0} onClick={()=>handleStackClick(4)}/>
          <CardRowGap/>
          <Pile cards={state.draw} spacing={0} onClick={handleDrawClick}/>
          <Pile cards={state.discard} spacing={0} onClick={handleDiscardClick}/>
      </CardRow>
      <CardRow>
          <Pile cards={state.pile1} onClick={()=>handlePileClick(1)}/>
          <Pile cards={state.pile2} onClick={()=>handlePileClick(2)}/>
          <Pile cards={state.pile3} onClick={()=>handlePileClick(3)}/>
          <Pile cards={state.pile4} onClick={()=>handlePileClick(4)}/>
          <Pile cards={state.pile5} onClick={()=>handlePileClick(5)}/>
          <Pile cards={state.pile6} onClick={()=>handlePileClick(6)}/>
          <Pile cards={state.pile7} onClick={()=>handlePileClick(7)}/>
      </CardRow>
      <div id="errorMsg"></div>
</GameBase>
function handleGameBaseClick() {
  console.log("You clicked somewhere on the board!");
}

function handleStackClick(id) {
  console.log("You clicked on stack:", id);
}

function handleDrawClick() {
  console.log("You clicked on the draw pile");
}

function handleDiscardClick() {
  console.log("You clicked on the discard pile");
}

function handlePileClick(id) {
  console.log("You clicked on pile:", id);
}

It may even be simpler to create separate functions for each pile / stack rather than a parameter.为每个堆/堆栈创建单独的函数而不是参数甚至可能更简单。

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

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