在React.js中选择随机样式的组件

时间:2018-07-25 23:49:16

标签: javascript reactjs components

我可能想为自己的投资组合网站做一件不可能的事情。我想使用React-Burger-Menu库并导入所有菜单组件,并在屏幕加载时随机选择一个。我决定创建一个包含所有导入内容的数组,并使用math.random函数查看是否可行。当然不是。我知道这可能真的很愚蠢,但是我认为如果可行的话,它会很酷。这是到目前为止我对随机函数的了解:

import { slide as Menu0 } from 'react-burger-menu';
import { stack as Menu1 } from 'react-burger-menu';
import { elastic as Menu2 } from 'react-burger-menu';
import { bubble as Menu3 } from 'react-burger-menu';
import { push as Menu4 } from 'react-burger-menu';
import { pushRotate as Menu5 } from 'react-burger-menu';
import { scaleDown as Menu6 } from 'react-burger-menu';
import { scaleRotate as Menu7 } from 'react-burger-menu';
import { fallDown as Menu8 } from 'react-burger-menu';
import { reveal as Menu9 } from 'react-burger-menu';

Menus = Array[Menu0, Menu1, Menu2, Menu3, Menu4, Menu5, Menu6, 
Menu7, Menu8, Menu9]

var Menu = Menus[Math.floor(Math.random() * Menus.length)];

export default Menu;

就像文档中所说的那样,我正在用<Menu></Menu>标签包装导航。这是代码:

import Menu from './sidebar';
import Contact from "../pages/contact";
import Portfolio from "../pages/portfolio";
import Index from "../pages/index";
import "../styles/customnav.css";

class customnav extends Component {
  render() {
    return (
      <Menu>
        <HashRouter>
          <div>
           <nav className="stroke">
            <ul className="menu">
            <li>
              <NavLink className="menu-item" to="/">
                Home
            </NavLink>
            </li>
            <li>
              <NavLink className="menu-item" to="/portfolio">
                Portfolio
            </NavLink>
            </li>
            <li>
              <NavLink className="menu-item" to="/contact">
                Contact
            </NavLink>
            </li>
          </ul>

          <Route exact path="/" component={Index} />
          <Route path="/portfolio" component={Portfolio} />
          <Route path="/contact" component={Contact} />
          </nav>
         </div>
      </HashRouter>
     </Menu>
    );
 }
}

导出默认的customnav;

这是错误代码: Cannot read property 'length' of undefined

我知道这很麻烦,但谢谢您的帮助。

2 个答案:

答案 0 :(得分:1)

使用Array创建数组时,需要使用Array()而不是Array[]

enter image description here

演示

console.log(Array[1,2,3]);
console.log(Array(1,2,3));
// OR
console.log([1,2,3]);

答案 1 :(得分:1)

数组语法错误,您可以这样做

import { slide as Menu0 } from 'react-burger-menu';
import { stack as Menu1 } from 'react-burger-menu';
import { elastic as Menu2 } from 'react-burger-menu';
import { bubble as Menu3 } from 'react-burger-menu';
import { push as Menu4 } from 'react-burger-menu';
import { pushRotate as Menu5 } from 'react-burger-menu';
import { scaleDown as Menu6 } from 'react-burger-menu';
import { scaleRotate as Menu7 } from 'react-burger-menu';
import { fallDown as Menu8 } from 'react-burger-menu';
import { reveal as Menu9 } from 'react-burger-menu';

const Menus = [Menu0, Menu1, Menu2, Menu3, Menu4, Menu5, Menu6, 
Menu7, Menu8, Menu9];

const Menu = Menus[Math.floor(Math.random() * Menus.length)];

export default Menu;