简体   繁体   English

当我单击一个按钮时,它同时打开所有按钮

[英]When i click one button its open all buttons simultaneously

When I click on the post button it opens all buttons like media and user.i tried to define state function but when i click one button all buttons are open simultaneously.当我单击发布按钮时,它会打开所有按钮,例如媒体和用户。我尝试定义 state function 但是当我单击一个按钮时,所有按钮都会同时打开。

this problem i am facing我面临的这个问题

My code are here我的代码在这里

export default function ListItem() {导出默认 function ListItem() {

const [open, setOpen] = React.useState(false);


const handleClick = () => {
    setOpen(!open);
};


return (
    <div>
        <List
            sx={{ width: '100%', maxWidth: 360, bgcolor: 'background.paper' }}
            component="nav"
            aria-labelledby="nested-list-subheader"
        >
            {/* dashbord */}
            <ListItemButton component="a" href="#simple-list">
                <ListItemIcon>
                    <DashboardTwoToneIcon />
                </ListItemIcon>
                <ListItemText primary="DashBoard" />
            </ListItemButton>

            {/* post */}
            <ListItemButton onClick={handleClick}>
                <ListItemIcon>
                    <PostAddTwoToneIcon />
                </ListItemIcon>
                <ListItemText primary="Post" />
                {open ? <ExpandLess /> : <ExpandMore />}
            </ListItemButton>
            <Collapse in={open} timeout="auto" unmountOnExit>
                <List component="div" disablePadding>
                    <ListItemButton sx={{ pl: 4 }} component="a" href="#">
                        <ListItemText primary="New post" component="a" />
                    </ListItemButton>
                    <ListItemButton sx={{ pl: 4 }} component="a" href="#">
                        <ListItemText primary="All posts" />
                    </ListItemButton>
                </List>
            </Collapse>

            {/* Media */}
            <ListItemButton onClick={handleClick}>
                <ListItemIcon>
                    <SubscriptionsTwoToneIcon />
                </ListItemIcon>
                <ListItemText primary="Media" />
                {open ? <ExpandLess /> : <ExpandMore />}
            </ListItemButton>
            <Collapse in={open} timeout="auto" unmountOnExit>
                <List component="div" disablePadding>
                    <ListItemButton sx={{ pl: 4 }} component="a" href="#">
                        <ListItemText primary="All Media" component="a" />
                    </ListItemButton>
                    <ListItemButton sx={{ pl: 4 }} component="a" href="#">
                        <ListItemText primary="Add New Media" />
                    </ListItemButton>
                </List>
            </Collapse>

You call function setOpen on a click on any object, however setOpen seems to change the state of a global variable that is used to determine whether the contents should be shown or not.您在单击任何 object 时调用 function setOpen,但是 setOpen 似乎更改了用于确定是否应显示内容的全局变量的 state。 Try to set a unique variable as open state for every button and only change this variable with a function in onClick尝试为每个按钮设置一个唯一变量为 open state 并且仅使用 onClick 中的 function 更改此变量

You are using a single boolean state for all of them.您对所有这些都使用单个 boolean state。 I suggest storing the open status of each item/element you want to toggle in an object and check if the specific item/element is currently toggled open.我建议将要切换的每个项目/元素的打开状态存储在 object 中,并检查特定项目/元素当前是否处于打开状态。

const [open, setOpen] = React.useState({});

const handleClick = (id) => () => {
  setOpen(open => ({
    ...open,
    [id]: !open[id],
  }));
};

... ...

<ListItemButton onClick={handleClick("post")}> // <-- pass id
  <ListItemIcon>
    <PostAddTwoToneIcon />
  </ListItemIcon>
  <ListItemText primary="Post" />
  {open["post"] ? <ExpandLess /> : <ExpandMore />} // <-- check by id
</ListItemButton>
<Collapse
  in={open["post"]} // <-- check by id
  timeout="auto"
  unmountOnExit
>
  <List component="div" disablePadding>
    <ListItemButton sx={{ pl: 4 }} component="a" href="#">
      <ListItemText primary="New post" component="a" />
    </ListItemButton>
    <ListItemButton sx={{ pl: 4 }} component="a" href="#">
      <ListItemText primary="All posts" />
    </ListItemButton>
  </List>
</Collapse>

Apply same for other buttons and collapsables but using specific unique keys.对其他按钮和可折叠项应用相同,但使用特定的唯一键。

暂无
暂无

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

相关问题 我在一个组件中同时使用两个按钮,当我单击它而不是另一个按钮时,我只希望一个按钮起作用 - i am using two buttons simultaneously in one component, i want only one button to work when i click it not the other button 单击一个按钮关闭所有按钮 - All buttons close on click of one button 如何在 React 中实现多个按钮,当您单击一个按钮时,所有其他按钮都被取消选择 - How to implement multiple buttons in React and when you click one button, all the other buttons are deselected 当我单击特定的“addToBasket”按钮时,我不想更改所有“addToBasket”按钮。 只有点击的按钮应该改变 - I do not want all the "addToBasket" buttons to change when i click on a specefic "addToBasket" button. Only the clicked button should change React - 从 100 个按钮数组中更改一个按钮的 state,并一键重置所有按钮的 state - React - change state of one button from 100 buttons array, and reset state of all buttons on one click 我怎样才能点击特定按钮而不是所有按钮 - How can I how to click specific button but not all buttons 单击反应中的一个按钮时,如何禁用所有按钮? - How to get all buttons disabled, when one button is clicked in react? 这是我必须通过各种按钮显示信息的代码。 问题是当我单击一个按钮时,其他按钮会显示信息 - Here is code where I have to display information through various buttons. The issue is that when I click one button, the other buttons display info JavaScript React Front-End:打开一个折叠按钮,所有折叠按钮都打开而不是一个 - JavaScript React Front-End: Opening one collapse button, all collapse buttons open instead of just the one 为什么单击单个删除按钮时除了一个图像之外的所有图像都被删除? - Why are all the images being deleted except for one when I click single delete button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM