2

I have a basic menu imported from react semantic-ui

<Menu secondary vertical>
        <Menu.Item
          name='account'
          active={activeItem === 'account'}
          onClick={this.handleItemClick}
        />

I obviously have more items and I modified such that each item from the menu links to a different page:

<Menu secondary vertical>
         <Link to="/account">
         <Menu.Item
          name='account'
          active={activeItem === 'account'}
          onClick={this.handleItemClick}
        />
        </Link>
        
 

The code obviously works as expected however I am getting the error

<a> cannot appear as a descendant of <a>

So I modified the code to <Menu.item as='div' and it still works however it loses some functionalities. Basically before I was able to hover on the menu item and it would show the result in the picture. To get on "account" the same result, I have to click twice. If I leave Menu.item as 'a' it works. Any way I could fix that? Picture 1

2 Answers 2

1

You should not be using Link and Menu.Item like that.

You could do something similar to what you already did with as property. You could say

<Menu.Item 
   as={Link}
   to={"/account"}
   name='account'
   active={activeItem === 'account'}
   onClick={this.handleItemClick}
>
    Account
</Menu.Item>

This means you need to import {Link} from react-router-dom which I suppose you already did

Sign up to request clarification or add additional context in comments.

2 Comments

Can I find out why was my answer down voted since it is perfectly functioning in the projects that I am making with react semantic ui?
It works for me as well. Thanks for sharing. I didn't downvote, I can't upvote either since my account is brand new
0

A workaround (if you do not need anchor tag for some reason) is to handle it in method handleItemclick:

handleItemClick = (e) => {
  //..code
  this.props.history.push('/myroute');
}

4 Comments

he does not need anchor tag. Menu.Item is anchor tag if you look at the DOM structure.
@LazarNikolic his problem indeed is that both Link and Menu.Item render as anchor. If he want a fast solution and avoid css adjustment this will work fine without using <Link />
@LazarNikolic your solution is valid, too
Thanks, I consider it

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.