0

I'm using React with Material-UI. I'm developing a simple UI in a form of a dropdown menu. I'd like to control the rendering of the first row with condition passed as a prop. How can I use the condition to render or skip rendering of the first row?

export const NativeSdkMenu = memo(({ showFirstMenuItem }) => (
  <Menu>
    <MenuItem component="a" href={link1}>
      {"First"}
    </MenuItem>
    <MenuItem component="a" href={link1}>
      {"Second"}
    </MenuItem>
    <MenuItem component="a" href={link1}>
      {"Third"}
    </MenuItem>
    <MenuItem component="a" href={link1}>
      {"Fourth"}
    </MenuItem>
  </Menu>
));

Obviously this doesn't work:

if (showFirstMenuItem) {
    <MenuItem component="a" href={link1}>
      {"First"}
    </MenuItem>
}

I've tried a few similar approaches, but it looks like I need to use some specific JSX syntax which I don't know.

3

3 Answers 3

4

Based on your sample and assuming your prop showFirstMenuItem is a boolean you can simply use the && operator to show the first item if your showFirstMenuItem is true:

export const NativeSdkMenu = memo(({ showFirstMenuItem }) => (
 <Menu>
  {showFirstMenuItem && (
    <MenuItem component="a" href={link1}>
      {"First"}
   </MenuItem>)}
   <MenuItem component="a" href={link1}>
    {"Second"}
   </MenuItem>
   <MenuItem component="a" href={link1}>
     {"Third"}
   </MenuItem>
   <MenuItem component="a" href={link1}>
    {"Fourth"}
   </MenuItem>
  </Menu>
  ));
Sign up to request clarification or add additional context in comments.

3 Comments

Excellent solution. Enclosing the whole menuitem into parenthesis () was the root cause of the few of my approaches not working.
You're welcome, you can take a look on this article to see some other approaches for conditional rendering: robinwieruch.de/conditional-rendering-react
Thanks, I've seen that article actually before trying few of my approaches...
2

Quite simple actually

export const NativeSdkMenu = memo(({ showFirstMenuItem }) => (
  <Menu>
   {showFirstMenuItem && <MenuItem component="a" href={link1}>
   {"First"}
  </MenuItem>}
  <MenuItem component="a" href={link1}>
   {"Second"}
  </MenuItem>
  <MenuItem component="a" href={link1}>
   {"Third"}
  </MenuItem>
  <MenuItem component="a" href={link1}>
   {"Fourth"}
  </MenuItem>
</Menu>
));

1 Comment

Good direction, but also I had to add ( and ) around the whole MenuItem to make it compile.
1

You can set css class conditionnally with your prop value :

<MenuItem component="a" href={link1} className={this.props.showFirstMenuItem ? style['your-class-name'] : ''}>
  {"First"}
</MenuItem>

3 Comments

I'd prefer react-based solution, so that I won't have to modify styles / create classes, but thanks for the help!
Srry I missread i thought u want to render in a different way and not just show / hide !!
No problem. I wanted just to hide/show the first (or any other item) in the menu by controlling it with a prop that passed externally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.