Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!