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

168
Views
How to check if component returns false in react?

I have the following code which I'm trying to refactor

const [edit, setEdit] = useState(false);
<ListItemText
        primary={edit ? 
            <TextareaAutosize ... />
           : <Typography ..../>}
      />

The TextareaAutosize repeats a lot through the app so I thought I would convert it into a separate component.

export default function Textarea(props) {
    const [edit, setEdit] = useState(false);
    if(edit){
        return(
            <TextareaAutosize .../>
        )
    }
    return false
}

As you can see, if edit is false the component returns false. The back at the top I thought I would do this

import Textarea from "../Textarea";
<ListItemText
        primary={Textarea ? 
            <Textarea ... />
           : <Typography ..../>}
      />

By my logic, there should be a way to check if component Textarea will return false, right?

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

0

A "component" (JSX) is a UI element. You must return JSX or null, and the rendering engine will render the returned JSX or nothing if null.

You can check a flag and short-circuit:

{isFlag && <MyComponent ... />}

or have the component render or not (or if you want to keep state just show/hide):

<MyComponent isVisible={isFlag} />

Or possible return different components depending on flags:

if (isLoading) {
  return <MyLoadingSkeletonComponent />
}
<MyMainUIComponent />

EDIT: You may want to refactor into a compound component where you take the flag and hide the show/hide logic within it

const TextArea = ({edit, editMode, viewMode}) => (
  edit ? editMode : viewMode
)

// Call it with the flag and both components:

<TextArea
   edit={edit}
   editMode={<TextareaAutosize ... />}
   viewMode={<Typography ... />}
/>
about 4 years ago · Juan Pablo Isaza Report

0

Have the best of the two worlds, your idea of refactoring is correct, but it is missing something.

try something like this:

export default function Textarea(props) {
  props.edit ? <TextareaAutosize .../> : <Typography ..../>
}

and

<ListItemText
        primary=<TextareaAutosize {edit} ... />
      />

In this case you have one place to change TextareaAutosize and Typography codes, less messy code in parent listing.

Hope this helps.

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!