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

234
Views
How to render two const components in a conditional case in React JS?

I have declared 2 constant components in my React functional component. I am trying to render them based on a condition,

      <Flex.Box w="90px" ml={1}>
        { mycondition
          ? ({ staticButton })
          : ((
            { staticButton })({ conditionalButton }))}
      </Flex.Box>

I am trying to render based on if mycondition is true or false. But, I am getting the below error in the console.

TypeError: {(intermediate value)} is not a function

Am I doing anything wrong?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can't really do what you're asking here.

  1. If you're creating components inside another component you still have to use Pascal Case.

  2. You still need to wrap the component name in < />. You can't just return a function.

  3. Adjacent components need to be in a container.

function Example() {

  const condition = false;

  const StaticButton = () => <div>Static</div>;
  const ConditionalButton = () => <div>Conditional</div>;

  return (
    <div>
      {condition
        ? <StaticButton />
        : (<div>
            <StaticButton />
            <ConditionalButton />
          </div>)
       }
    </div>
  );
};

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 Report

0

When you want to render 2 components at the same time in a ternary, you should wrap them inside a fragment (<>{staticButton} {conditionalButton}<>). I guess your 2 const are components pass by props ?

about 4 years ago · Juan Pablo Isaza Report

0

It seems you can move the staticButton out of condition:

<Flex.Box w="90px" ml={1}>
  {staticButton}
  {mycondition ? conditionalButton : null}
</Flex.Box>
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!