简体   繁体   English

我有多个 onClick 按钮,每当我单击 1 个按钮时,所有相同的按钮都会触发。 Reactjs

[英]I have multiple onClick button and whenever I click 1 button all the same buttons triggers. Reactjs

All of my components are the same here's a sample.我的所有组件都相同,这是一个示例。

 const [show, setShow] = useState(false);

  

    return (
        <Box align="center">
            <Box pb={2}>
                <Box align="left">
                    <h1>UI Elements</h1>
                </Box>
                <Card>
                    <CardContent>
                        <h2 align = 'Left'><FormatColorFillIcon/> Colors</h2>
                        <hr/>
                        <App/>
                    </CardContent>
                </Card>
                <IconButton onClick={()=> setShow(!show)} aria-label="Hello sir" align = "right">
                <CodeIcon/>
                </IconButton>
                { show ? 
                    <div><Paper className = {classes.paperColor}>
                            <SyntaxHighlighter classes = {classes.paperColor} language="javascript" style={docco} align = "left" style = {dracula}> 
                                {codeString}
                            </SyntaxHighlighter>
                        </Paper>
                    </div> : null}

I want to trigger the only button that i clicked.我想触发我点击的唯一按钮。 I'm sorry i'm new at react js对不起,我是 react js 的新手

My output wheneve i click the button it shows the code snippet text also on the above button toggle我的 output 每当我单击按钮时,它也会在上面的按钮切换上显示代码片段文本

Pass a different function for each button's onClick (ie onIconButtonClick, onSubmitButtonClick, etc.)为每个按钮的onClick(即onIconButtonClick、onSubmitButtonClick等)传递不同的function

<IconButton onClick={onIconButtonClick} aria-label="Hello sir" align = "right">
  

Then define the functions like this:然后像这样定义函数:

const onIconButtonClick = () => {
  setShow(!show)
}

const onSubmitButtonClick = () => {
  //logic to submit
}

EDIT 1编辑 1

try this尝试这个

//for first icon button
<IconButton id="thing1" onClick={onIconButtonClick} aria-label="Hello sir" align = "right"></IconButton>


//for second icon button
<IconButton id="thing2" onClick={onIconButtonClick} aria-label="Hello sir" align = "right"></IconButton

const onIconButtonClick = (event) => {
  switch(event.currentTarget.id) {
    case "thing1":
      setShowThing1(!showThing1);
      break;
    case "thing2":
      setShowThing2(!showThing2);
      break;
    //...
    

  }
}


{ showThing1 ? 
   <div>
     <Paper className = {classes.paperColor}>
       <SyntaxHighlighter classes = {classes.paperColor} language="javascript" style={docco} align = "left" style = {dracula}> 
           {codeString}
       </SyntaxHighlighter>
     </Paper>
   </div> : 
  null}

To each one be independent you could abstract the snippetCode to a component where each one has its own show state:对于每个都是独立的,您可以将片段代码抽象为一个组件,其中每个组件都有自己的show state:

const CodeSnippet = ({ codeString }) => {
  const [show, setShow] = useState(false)

  return (
  <>
    <IconButton onClick={()=> setShow(!show)} aria-label="Hello sir" align = "right">
    <CodeIcon/>
    </IconButton>
    { show ? 
        <div>
          <Paper className = {classes.paperColor}>
            <SyntaxHighlighter classes = {classes.paperColor} language="javascript" style={docco} align = "left" style = {dracula}> 
                {codeString}
            </SyntaxHighlighter>
          </Paper>
        </div> : null
    }
  </>
  )
}

then you would call at your Parent component:然后你会调用你的父组件:

return (
    <Box align="center">
        <Box pb={2}>
            <Box align="left">
                <h1>UI Elements</h1>
            </Box>
            <Card>
                <CardContent>
                    <h2 align = 'Left'><FormatColorFillIcon/> Colors</h2>
                    <hr/>
                    <App/>
                </CardContent>
            </Card>
            <CodeSnippet codeString={codeString} />

暂无
暂无

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

相关问题 如果我将相同的 onClick 函数附加到多个按钮,如何使用 onClick 函数定位特定按钮的样式? - How can i target specific button's styling with onClick function if i have the same onClick function attached to multiple buttons? 我第一次单击按钮时未触发Reactjs-onClick事件 - Reactjs-onClick event not firing the first time i click button 如果我单击按钮然后在 reactJs 中显示此按钮 ID - if i click button then show this button id in reactJs 当我单击一个按钮时,它同时打开所有按钮 - When i click one button its open all buttons simultaneously 复选框需要单击 2 次以使其检查我已经尝试使用 onclick 以及它给出相同的行为我需要禁用按钮 oncheck - checked box need to be click 2 time to make it check i have tried with onclick as well it is giving same behaviour i need to disable button oncheck 如何在 ReactJS 中单击按钮 - How can I focus a button on click in ReactJS 每当我单击连接元掩码按钮时,为什么它会连接 coinbase 钱包? - Whenever i click on connect metamask button why it connects the coinbase wallet? 我的 React 应用程序中有按钮和教科书,如何使单击按钮触发文本输入? - I have buttons and a textbook in my React App, How do I make the click of the button trigger text input? 在 reactjs onclick 事件中更新了所有按钮 - Got all button updated in reactjs onclick event onClick 按钮触发所有组件打开 - Reactjs - onClick of button triggering all the components to open - Reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM