0

in that case if you first time click show its show hello world its right but again if you click on show its should not work. now its became a toggle but how can I do after one click its not working means after showing the hello world its should not again hide if you want to click again. I'm also use pointer Event its also not working

import React, { useState } from "react";
export default function DropDown() {
  const [show,setShow]=useState(false)
  return (
    <>
      {show?<h1>hello world</h1>:null}
      <div onClick={()=>{setShow(!show);return false;}}><span>show</span></div>
    </>
  )
}
1
  • You need to disable the div after the first click. Is that the ask? Commented Apr 6, 2022 at 8:08

1 Answer 1

1

You can simply modify your onClick with one time value set

onClick={()=>{setShow(true)}} //one time set

Full code

import React, { useState } from "react";
export default function DropDown() {
  const [show,setShow]=useState(false)
  return (
    <>
      {show?<h1>hello world</h1>:null}
      <div onClick={()=>{setShow(true)}}><span>show</span></div>
    </>
  )
}

If you want to disable that button after show update, you can set disabled={show} to that button

import React, { useState } from "react";
export default function DropDown() {
  const [show,setShow]=useState(false)
  return (
    <>
      {show?<h1>hello world</h1>:null}
      <div onClick={()=>{setShow(true)}} disabled={show}><span>show</span></div>
    </>
  )
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.