P
P
PlasterTom2018-01-28 17:15:17
React
PlasterTom, 2018-01-28 17:15:17

How to make a menu in Semantic UI?

The documentation has a ready-made vertical menu code

import React, { Component } from 'react'
import { Menu } from 'semantic-ui-react'

export default class MenuExampleVerticalText extends Component {
  state = { activeItem: 'closest' }

  handleItemClick = (e, { name }) => this.setState({ activeItem: name })

  render() {
    const { activeItem } = this.state

    return (
      <Menu text vertical>
        <Menu.Item header>Sort By</Menu.Item>
        <Menu.Item name='closest' active={activeItem === 'closest'} onClick={this.handleItemClick} />
        <Menu.Item name='mostComments' active={activeItem === 'mostComments'} onClick={this.handleItemClick} />
        <Menu.Item name='mostPopular' active={activeItem === 'mostPopular'} onClick={this.handleItemClick} />
      </Menu>
    )
  }
}

And how to combine it with routing? Should I use the Link tag? (Example from the tutorial)
import React from 'react'
import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom'

// Each logical "route" has two components, one for
// the sidebar and one for the main area. We want to
// render both of them in different places when the
// path matches the current URL.
const routes = [
  { path: '/',
    exact: true,
    sidebar: () => <div>home!</div>,
    main: () => <h2>Home</h2>
  },
  { path: '/bubblegum',
    sidebar: () => <div>bubblegum!</div>,
    main: () => <h2>Bubblegum</h2>
  },
  { path: '/shoelaces',
    sidebar: () => <div>shoelaces!</div>,
    main: () => <h2>Shoelaces</h2>
  }
]

const SidebarExample = () => (
  <Router>
    <div style={{ display: 'flex' }}>
      <div style={{
        padding: '10px',
        width: '40%',
        background: '#f0f0f0'
      }}>
        <ul style={{ listStyleType: 'none', padding: 0 }}>
          <li><Link to="/">Home</Link></li>
          <li><Link to="/bubblegum">Bubblegum</Link></li>
          <li><Link to="/shoelaces">Shoelaces</Link></li>
        </ul>

        {routes.map((route, index) => (
          // You can render a <Route> in as many places
          // as you want in your app. It will render along
          // with any other <Route>s that also match the URL.
          // So, a sidebar or breadcrumbs or anything else
          // that requires you to render multiple things
          // in multiple places at the same URL is nothing
          // more than multiple <Route>s.
          <Route
            key={index}
            path={route.path}
            exact={route.exact}
            component={route.sidebar}
          />
        ))}
      </div>

      <div style={{ flex: 1, padding: '10px' }}>
        {routes.map((route, index) => (
          // Render more <Route>s with the same paths as
          // above, but different components this time.
          <Route
            key={index}
            path={route.path}
            exact={route.exact}
            component={route.main}
          />
        ))}
      </div>
    </div>
  </Router>
)

export default SidebarExample

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-01-28
@PlasterTom

return (
  <Menu text vertical>
    <Menu.Item
      name="feed"
      active={activeItem === 'feed'}
      onClick={this.handleItemClick}
    >
      <Link to="/feed">Feed</Link>
    </Menu.Item>
  </Menu>
);

M
Miniwe, 2018-08-07
@jumbastic

<Menu.Item as={Link} to='/feed'>Feed</Menu.Item>
or

<Menu.Item as={NavLink} activeClassName="active" to='/feed'>Feed</Menu.Item>
to hook the active class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question