Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
How to return a DOM's element with a handleClick function in ReactJS

I'm learning ReactJS and I was trying to return a simple Hello World to my DOM when I click the button.

Also there's no return error in console, and my console.log() is returning the right value when I click.

const Button = () => {

  function handleCLick() {
    console.log('Active')
    return <h1>Hello World</h1>
  }

  return (
    <button onClick={handleCLick}>Clique aqui</button>
  )
}

const App = () => {
  return <Button />
};

export default App; 

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Return to where? The browser's event handling code is calling handleClick. Your code is nowhere near where the return value gets passed to.

If you want to display some content in response to a click event then:

  • Use the useState hook to create a state
  • Change the state using the click event handler
  • Use some logic to return different content from the component based on the value of the state

This is covered in the React manual

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this:

import React, { useState } from 'react';

const App = () => {
  const [display, setDisplay] = useState(null);

  const handleClick = () => {
     setDisplay(display ? null : 'display');
  }
  
  return (
    <>
     {display && <h1>"Hello world"</h1>}
     <button onClick={handleCLick}>Clique aqui</button>
   </>
  ;
};

export default App;
about 4 years ago · Juan Pablo Isaza Denunciar

0

  1. Add state - in this simple example we're just updating the h1 element.

  2. Move the handler from the button to the parent component, and have that pass the handler down to the Button component so that when it's clicked, that function is called.

  3. When the handler is called update the state, and the component will render again, and h1 will have the updated value.

const { useState } = React;

function Example() {
  
  // Initialise your state
  const [h1, setH1] = useState('Nothing to see here.');
  
  // Set a new state when the handler is called
  function handleClick() {
    setH1('Hello World');
  }

  // Pass the handler function to `Button`
  // in its props. `h1` will contain the state,
  // and will be updated when the state changes
  return (
    <div>
      <h1>{h1}</h1>
      <Button handleClick={handleClick} />
    </div>
  );
  
}

// `Button` accepts the `handleClick` function
// and calls it when it's clicked
function Button({ handleClick }) {
  return (
    <button
      onClick={handleClick}
    >Clique aqui
    </button>
  );
}

ReactDOM.render(
  <Example />,
  document.getElementById('react')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda