gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:50:58 27 4
gpt4 key购买 nike

我可能想为我的作品集网站做一件不可能的事情。我想使用 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>
);
}
}

导出默认自定义导航;

错误代码如下: Cannot read property 'length' of undefined

我知道这有些牵强,但感谢您的帮助。

最佳答案

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

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;

关于javascript - 在 React.js 中选择随机样式的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51529170/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com