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

250
Vistas
ReactJS - Passing a component as a return

Im using React, useState and React-Icons.

My intention is to create a dropdown menu (without Bootstrap). I want to change the icon when clicked (to init the function) but the output is the raw SVG details. Any ideas?

import { IoMdArrowDropdownCircle, IoMdArrowDropupCircle } from "react-icons/io";
function navBar() {
    
  const navToggle = () => {
    const [navMenuToggle, changeNavToggle] = useState(true);
       return navMenuToggle ? (
         <IoMdArrowDropdownCircle />
       ) : (
         <IoMdArrowDropupCircle />
       );
    changeNavToggle(false);
  };

  return (
    <>
      <div className="tableIcon" id="navDropdown" onClick={navToggle}>
        <IoMdArrowDropdownCircle />
      </div>
    </>
  );
}

export default navBar;
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You should never use react hooks inside a function , they should always be in the react component before the return .

I'd suggest the easiest way to render a component conditionally like this :

export default function RenderComponent() {
  const [show, setShow] = React.useState(false);

  
  return show ? <ComponentOne /> : <ComponentTwo />;
}

Or this way :

export default function RenderComponent() {
  const [show, setShow] = React.useState(false);

  const render = () => {
   return show ? <ComponentOne /> : <ComponentTwo />
  }
  
  return render() ;
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

I managed to solve this issue by making a clickhandler function to change the state and a function to return the icon.

  <div className="tableIcon" id="navDropdown" onClick={navToggle}>
     {createIcon()}
  </div>

functions

  const [navMenuToggle, changeNavToggle] = useState("true");
  const navToggle = () => {
    if (navMenuToggle == "true") {
      changeNavToggle("false");
      document.getElementById("menuDD").style.visibility = "visible";
    } else {
      changeNavToggle("true");
      document.getElementById("menuDD").style.visibility = "hidden";
    }
  };

  const createIcon = () => {
    if (navMenuToggle == "true") {
      return <IoMdArrowDropdownCircle />;
    } else {
      return <IoMdArrowDropupCircle />;
    }
  };
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