[英]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.