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 deselect the values from state after selecting

I am building an app in this after selecting the specific NFT a new div with extra content will show up. The selecting part is working fine but now I want it to be able to deselect it.
This is the state

const [selectedNFT, setSelectedNFT] = useState<NFT>();

and this is the function for selecting the NFT

{ownedNfts?.data?.map((nft) => (
        <div
          onClick={() => setSelectedNFT(nft)}
          className={`flex flex-col space-y-2 card min-w-fit border-2 bg-gray-100 
          ${
            nft.metadata.id === selectedNFT?.metadata?.id
              ? "border-black scale-105 z-50"
              : "border-transparent"
          }`}
          key={nft.metadata.id}
        >

the onClick events populate the state but what I want is that after clicking on it again it should remove the data from the state making the state empty again

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

>Solution :

you can check if the nft is the current state, if so set it empty or whatever the desired value else set it to state .. (assuming you are putting only one nft in to selectedNFT)

onClick={() => {
   selectedNFT === nft ? setSelectedNFT("") : setSelectedNFT(nft)
}
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