I wanted to create an Electron app with React and MaterialUI.
The interface looks pretty, but nothing happens when I click on the Drawer buttons. Routing is implemented, but whenever I try to insert a inside or around a MenuItem, it just throws
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of
MenuBar.
Here's my code:
// App.js
import './assets/css/App.css';
import React, { Component } from 'react';
import { render } from 'react-dom';
import MenuBar from './components/MenuBar';
import { BrowserRouter as Router, Route, Link} from 'react-router-dom';
import About from "./screens/About";
import SomePage from "./screens/SomePage";
class App extends Component {
  render() {
    return (
      <Router>
        <div>
            <MenuBar></MenuBar>
        <ul>
            <li><Link to="/">Home</Link></li>
            <li><Link to="/about">About</Link></li>
            <li><Link to="/somepage">Topics</Link></li>
        </ul>
        <hr/>
        <Route exact path="/"/>
        <Route path="/about" component={About}/>
        <Route path="/somepage" component={SomePage}/>
        </div>
    </Router>
    );
  }
}
export default App;// MenuBar.js
// @flow weak
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withStyles, createStyleSheet } from 'material-ui/styles';
import Drawer from 'material-ui/Drawer';
import AppBar from 'material-ui/AppBar';
import Toolbar from 'material-ui/Toolbar';
import Divider from 'material-ui/Divider';
import List, { ListItem, ListItemIcon, ListItemText } from 'material-ui/List';
import Typography from 'material-ui/Typography';
import Button from 'material-ui/Button';
import IconButton from 'material-ui/IconButton';
import MenuIcon from 'material-ui-icons/Menu';
import Menu, { MenuItem } from 'material-ui/Menu';
import MenuDots from './MenuDots';
// Routing
import { Link } from 'react-router'
// Icons
import InboxIcon from 'material-ui-icons/Inbox';
import DraftsIcon from 'material-ui-icons/Drafts';
import StarIcon from 'material-ui-icons/Star';
import SendIcon from 'material-ui-icons/Send';
import MailIcon from 'material-ui-icons/Mail';
import DeleteIcon from 'material-ui-icons/Delete';
import ReportIcon from 'material-ui-icons/Report';
const styleSheet = createStyleSheet({
  root: {
    width: '100%',
  },
  flex: {
    flex: 1,
  },
  list: {
    width: 250,
    flex: 'initial',
  },
  listFull: {
    width: 'auto',
    flex: 'initial',
  }
});
class MenuBar extends Component {
  constructor() {
    super();
    this.state = {
      open: false
    }
  }
  //Toggle function (open/close Drawer)
  toggleDrawer(_state) {
    if (_state === true || _state === false) {
      this.setState({
        open: _state
      })
    } else {
      this.setState({
        open: !this.state.open
      })
    }
  }
  handleLeftOpen = () => this.toggleDrawer(true);
  handleLeftClose = () => this.toggleDrawer(false);
  render() {
    const classes = this.props.classes;
    const mailFolderListItems = (
      <div>
        <ListItem button>
          <ListItemIcon>
            <InboxIcon />
          </ListItemIcon>
          <Link to="/somepage"><ListItemText primary="test" /></Link>
        </ListItem>
        <ListItem button>
          <ListItemIcon>
            <StarIcon />
          </ListItemIcon>
          <ListItemText primary="Starred" />
        </ListItem>
        <ListItem button>
          <ListItemIcon>
            <SendIcon />
          </ListItemIcon>
          <ListItemText primary="Send mail" />
        </ListItem>
        <ListItem button>
          <ListItemIcon>
            <DraftsIcon />
          </ListItemIcon>
          <ListItemText primary="Drafts" />
        </ListItem>
      </div>
    );
    const otherMailFolderListItems = (
      <div>
        <ListItem button>
          <ListItemIcon>
            <MailIcon />
          </ListItemIcon>
          <ListItemText primary="All mail" />
        </ListItem>
        <ListItem button>
          <ListItemIcon>
            <DeleteIcon />
          </ListItemIcon>
          <ListItemText primary="Trash" />
        </ListItem>
        <ListItem button>
          <ListItemIcon>
            <ReportIcon />
          </ListItemIcon>
          <ListItemText primary="Spam" />
        </ListItem>
      </div>
    );
    const sideList = (
      <div>
        <List className={classes.list} disablePadding>
          {mailFolderListItems}
        </List>
        <Divider />
        <List className={classes.list} disablePadding>
          {otherMailFolderListItems}
        </List>
      </div>
    );
    return (
      <div className={classes.root}>
        <AppBar position="static">
          <Toolbar style={{ paddingLeft: "8px", paddingRight: "8px" }}>
            <IconButton color="contrast" aria-label="Menu" onClick={this.toggleDrawer.bind(this)}>
              <MenuIcon />
            </IconButton>
            <Typography type="title" color="inherit" className={classes.flex}>
              Some Software
          </Typography>
            <Button color="contrast">Login</Button>
            <MenuDots></MenuDots>
          </Toolbar>
        </AppBar>
        <Drawer
          open={this.state.open}
          onRequestClose={this.handleLeftClose}
          onClick={this.handleLeftClose}
        >
          {sideList}
        </Drawer>
      </div>
    );
  }
}
MenuBar.propTypes = {
  classes: PropTypes.object.isRequired,
};
export default withStyles(styleSheet)(MenuBar);You can use the component prop of Material-UI's Link to integrate with Link in react-router-dom . You can do the same thing with Material-UI's Button .
To make a React Material UI button act as a React Router Link, we can set the component prop of the Material UI Button to a React Router Link . to set the Button 's component prop to Link to render the button as a link with the to prop set as the to prop of the Link .
To add a link to a List with React Material UI, we can set the href prop of a ListItem component. We set the href prop of ListItem to a URL. And we set the component prop to 'a' to render the list item as a link. As a result, we should see a link rendered and it should go to https://www.google.com when we click on it.
Okay, so here's what I changed to achieve the following:
First, I had to change my imports from
import { Link } from 'react-router'
to
import { NavLink } from 'react-router-dom';
This allowed for better styling and more options. This is the code it boiled down to:
<NavLink to="/about" style={{ textDecoration: 'none', color: 'unset' }} >
  <ListItem button>
    <ListItemText primary="Test"/>
  </ListItem>
</NavLink >
textDecoration and color was necessary to remove the underline on links (because NavLink renders as a normal ) and to normalize the ripple effect.
You are using react router v4 so instead of importing Link from react-router in Menubar component import it from react-router-dom.
Like this:
import { Link } from 'react-router-dom';
Check the DOC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With