Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
Changing style of another element using react

I'm relatively new to react and am totally lost trying to figure out how to make an Component appear when I press on a button. I've set up the code as such

<Button>GO</Button>

<CalendarIcon id="calendar visibility="hidden"/>

and then useVisibility()

export default function useVisibility() {
    const[visibility, setVisibility] = useState("hidden")

    useEffect(() => {
        function handleVis(){
            setVisibility("visible")
        }
        button.addEventListener("onClick", handleVis)

        return () => button.removeEventListener("onClick", handleVis)
    }, [])
    return visibility
}

My problem is that I don't know how to pass the button into the function so that I can add the event listener. If I am doing this in a totally roundabout way or overcomplicating it please tell me because I am so lost.

Thanks!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

What I would do is let each instance where you render a button specify how its click handler should behave as there can be many use cases for a button in a website.

function MyComponent() {
  const[isVisible, setIsVisible] = useState(false)
  const handleVisibilityToggle = useCallback(
    () => setIsVisible(!isVisible),
    [isVisible, setIsVisible]
  )
  ...
  const visibility = isVisible ? 'unset' : 'hidden'
  return (
    <>
      ...
      <Button onClick={handleVisibilityToggle}>GO</Button>
      <CalendarIcon id="calendar" visibility={visibility}/>
    </>

  )
}

if you would like to clean up how that code is used and abstract the logic to a visibility hook it would look something like this

function useVisibilityToggle(defaultValue = false) {
    const[isVisible, setIsVisible] = useState(defaultValue)
    const toggleVisibility = useCallback(
      () => setIsVisible(!isVisible),
      [isVisible, setIsVisible]
    )

    const visibility = isVisible ? 'visible' : 'hidden'
    return [visibility, toggleVisibility]
}

function MyComponent() {
  const [visibility, toggleVisibility] = useVisibilityToggle(false)
  
  return (
    <>
      ...
      <Button onClick={toggleVisibility}>GO</Button>
      <CalendarIcon id="calendar" visibility={visibility}/>
    </>

  )
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Check the first example here: https://reactjs.org/docs/hooks-state.html

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

That should show you how to manipulate the state when the button is clicked. Next you wanted to show/hide something when that button is clicked.

First let's change that useState to a boolean one, so

const [hidden, setHidden] = useState(false);

Then we can change the button so that it uses the previous state to set the new one. You should never use the hidden in the setHidden, but instead pass a function into it, which gets the previous value as parameter.

<button onClick={() => setHidden(prevHidden => !prevHidden)}>

And now we want to use that value, so we can add something below the button:

{ !hidden ? (<p>This text is visible</p>) : (<></>) }

Working example: https://codesandbox.io/s/strange-williamson-wuhnb?file=/src/App.js

Your code looks like you are trying to build a custom hook, that's more advanced stuff, and if you are super new to React, you won't need that right now.

about 4 years ago · Juan Pablo Isaza Relatório

0

Your main goal is to show CalendarIcon component visible when you click on GO Button.

So you need a state lets say visible variable to control this. You can update this state onClick of your Button as shown below to true or false, And make visibility="visible" always.

When visible will be true your CalendarIcon will appear.

const [visible, toggleVisibility] = useState(false)

<Button onClick={()=> toggleVisibility(!visible)}>GO</Button>

{visible && <CalendarIcon id="calendar" visibility="visible"/>}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda