简体   繁体   English

我在 React 中创建了折叠,我为每次点击使用了不同的 useState,但我只想使用一个 useState

[英]I created collapse in React, I used different useState for each click, but I want to use only one useState

    function Solution() {
    const [firstClick,setFirstClick] = useState(true);
    const [secondClick,setSecondClick] = useState(true);
    const [thirdClick,setThirdClick] = useState(true);
    const [fourthClick,setFourthClick] = useState(true);
    const [fivethClick,setFivethClick] = useState(true);
    const [sixthClick,setSixthClick] = useState(true);
    const [seventhClick,setSeventhClick] = useState(true);

    const changeIconFirst = () => {
    firstClick ? setFirstClick(false) : setFirstClick(true);
    setSecondClick(true);
    setThirdClick(true);
    setFourthClick(true);
    setFivethClick(true);
    setSixthClick(true);
    setSeventhClick(true)

    }
    const changeIconSecond = () => {
    secondClick ? setSecondClick(false) : setSecondClick(true);
    setFirstClick(true);
    setThirdClick(true);
    setFourthClick(true);
    setFivethClick(true);
    setSixthClick(true);
    setSeventhClick(true)
    }
    const changeIconThird = () => {
    thirdClick ? setThirdClick(false) : setThirdClick(true);
    setFirstClick(true);
    setSecondClick(true);
    setFourthClick(true);
    setFivethClick(true);
    setSixthClick(true);
    setSeventhClick(true)
    }
    const changeIconFourth = () => {
    fourthClick ? setFourthClick(false) : setFourthClick(true);
    setFirstClick(true);
    setSecondClick(true);
    setThirdClick(true);
    setFivethClick(true);
    setSixthClick(true);
    setSeventhClick(true)
    }
    const changeIconFiveth = () => {
    fivethClick ? setFivethClick(false) : setFivethClick(true);
    setFirstClick(true);
    setSecondClick(true);
    setThirdClick(true);
    setFourthClick(true);
    setSixthClick(true);
    setSeventhClick(true)
    }
    const changeIconSixth = () => {
    sixthClick ? setSixthClick(false) : setSixthClick(true);
    setFirstClick(true);
    setSecondClick(true);
    setThirdClick(true);
    setFourthClick(true);
    setFivethClick(true);
    setSeventhClick(true)
    }
    const changeIconSeventh = () => {
    seventhClick ? setSeventhClick(false) : setSeventhClick(true);
    setFirstClick(true);
    setSecondClick(true);
    setThirdClick(true);
    setFourthClick(true);
    setFivethClick(true);
    setSixthClick(true);
    }

I used more than one useState to open collapse when I clicked it, but I want to set up a simpler and more dynamic structure by using one useState instead of more than one.我用了不止一个useState在点击时打开折叠,但我想通过使用一个useState而不是多个useState来建立一个更简单、更动态的结构。 By doing if else with the true false method, I changed the className to enable collapse to open and close.通过使用 true false 方法执行 if else,我更改了 className 以启用折叠以打开和关闭。 I did not think of it in the method.方法里没有想到。 I am waiting for your suggestions to create a more dynamic and simpler structure.我正在等待您的建议,以创建一个更动态、更简单的结构。

This code should work just fine if you have any questions go ahead and ask如果您有任何问题 go 提前询问,此代码应该可以正常工作

 function Solution() { //this is your main useState that will take control of your collapse const [collapse, setCollapse] = useState(true); // this is your collapse button you should give it an on click event <button onClick={() =>setCollapse(;collapse)}>collapse </button>; }

Edited:编辑:

 function Solution() { const [clicks, setClicks] = useState({ firstClick: true, secondClick: true, thirdClick: true, fourthClick: true, fivethClick: true, sixthClick: true, seventhClick: true, }); const changeIconFirst = () => { setClicks({ firstClick: clicks.firstClick? true: false, secondClick: true, thirdClick: true, fourthClick: true, fivethClick: true, sixthClick: true, seventhClick: true, }); }; const changeIconSecond = () => { setClicks({ secondClick: clicks.secondClick? true: false, firstClick: true, thirdClick: true, fourthClick: true, fivethClick: true, sixthClick: true, seventhClick: true, }); }; const changeIconThird = () => { setClicks({ thirdClick: clicks.thirdClick? true: false, firstClick: true, secondClick: true, fourthClick: true, fivethClick: true, sixthClick: true, seventhClick: true, }); }; const changeIconFourth = () => { setClicks({ fourthClick: clicks.fourthClick? true: false, firstClick: true, secondClick: true, thirdClick: true, fivethClick: true, sixthClick: true, seventhClick: true, }); }; const changeIconFiveth = () => { setClicks({ fivethClick: clicks.fivethClick? true: false, firstClick: true, secondClick: true, thirdClick: true, fourthClick: true, sixthClick: true, seventhClick: true, }); }; const changeIconSixth = () => { setClicks({ sixthClick: clicks.sixthClick? true: false, firstClick: true, secondClick: true, thirdClick: true, fourthClick: true, fivethClick: true, seventhClick: true, }); }; const changeIconSeventh = () => { setClicks({ seventhClick: clicks.seventhClick? true: false, firstClick: true, secondClick: true, thirdClick: true, fourthClick: true, fivethClick: true, sixthClick: true, }); }; }

If you only want to use one single component then u can accomplish by giving each collapse a index.如果你只想使用一个组件,那么你可以通过给每个折叠一个索引来完成。

in the render在渲染中

<button onClick={()=> setId(1)}>Click</button> // can be 1,2,3,4,5,6,7

in your collapse it can be something like在你的崩溃中它可能是这样的

<CollapseComponent show={id === 1} />
<CollapseComponent show={id === 2} />
<CollapseComponent show={id === 3} />

I notice the question speaks about clicking multiple times.我注意到这个问题是关于多次点击的。 so onClick can be changed to as follows所以onClick可以改成如下

const onClick = () => {
     if (id === 7) { //assuming there's 8 pics
        setId(0)
     } else {
        setId(prev => prev+1)
     }
}

<button onClick={onClick}>Click</button> 

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

相关问题 如何仅更改包含对象的 React useState 中的一个值并将其他值分配给其他值? - How can I change only one value in a React useState that contains an object and assign a different one to the rest? 我应该为每个属性使用单独的 useState - Should I use separate useState for each property 使用 React Quill 的 useState 和 onChange 无法正常工作,不知道如何解决 - useState and onChange with React Quill not working as I want it to, not sure how to fix React usestate 数组长度因我点击的项目而异 - React usestate array length varies depending on item i click 我应该导入 useState 和 useEffect 还是可以使用 React.useState 和 React.useEffect? - Should I import useState and useEffect or is it ok to use React.useState and React.useEffect? 如何评估数组中的每个元素并在一个条件下使用 useState()? - How can I evaluate each element in array and use useState() on one condition? 如何在下一个 useState 中访问一个 useState 的数据 - How can i access the data from one useState in the next useState 我想在子组件中访问父 useState - I want to access parent useState in child component 如何使用 forEach 将 React useState 推入数组? - How do I use forEach to push into an array with React useState? 不能在反应中使用 useState 或 useEffect 我还有什么? - Cannot use useState or useEffect in react what else do I have?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM