Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to activate only one of the three buttons

I made three buttons using ‘map’.
It also specified a function to change the false value to true when clicked.

However, if you press one button, all three buttons are activated.
How can I activate just one button?

false = #fff
true = blue

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const [funnalBtn, setFunnalBtn] = useState(false);
const hadleChangeBtnColor = () => {
setFunnalBtn(prev => !prev);
};

<FunnalTarget>
    {FUNNAL_DATA.map(data => {
      return (
        <FunnalButton
          key={data.id}
          text={data.text}
          hadleChangeBtnColor={hadleChangeBtnColor}
          funnalBtn={funnalBtn}
        />
      );
    })}
  </FunnalTarget>

enter image description here
enter image description here

>Solution :

You could define a selectedBtn state and pass the id of the selected target to it:

const [selectedBtn, setSelectedBtn] = useState(-1);

const hadleChangeBtnColor = (id) => {
    setSelectedBtn(id);
};

<FunnalTarget>
    {FUNNAL_DATA.map(data => {
      return (
        <FunnalButton
          key={data.id}
          text={data.text}
          hadleChangeBtnColor={() => setSelectedBtn(data.id)}
          funnalBtn={selectedBtn === data.id}
        />
      );
    })}
  </FunnalTarget>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading