0

I want to change my wrapper tag around content. Currently I am doing it like this:

{!filter && <Styled.DropdownMenu>
    {Options}
</Styled.DropdownMenu>}
{filter && <Dropdown.Menu as={CustomMenu}>
   {Options}
</Dropdown.Menu>}

Which works. But since the content Options is always going to be same. I was wondering if there is a way to dynamically change just the wrapper tag in same line without repeating the code for {Options}.

2 Answers 2

1

Try this way

const TAG = !filter ? Styled.DropdownMenu : Dropdown.Menu;

const customMenuData = !filter ? {} : {CustomMenu};

<TAG as={customMenuData}>{Options}</TAG>
Sign up to request clarification or add additional context in comments.

1 Comment

How to pass as={CustomMenu} for Dropdown.Menu then?
0

You can use the following instead of multiple !filter checks.

filter ? <Styled.DropdownMenu>{Options}</Styled.DropdownMenu> 
: (<Dropdown.Menu as={CustomMenu}>
       {Options}
    </Dropdown.Menu>)

Or you can create separate components and use as follows

filter ? <StyledDropDownComponent options={Options} /> : <DropDownComponent options={Options} /> 

Which makes your code cleaner.

Comments

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.