简体   繁体   English

Reactstrap 导航栏切换不在 NextJS TypeScript 项目中切换

[英]Reactstrap navbar toggle not toggling in NextJS TypeScript project

I've installed Reactstrap on a project using the NextJS TypeScript install.我已经使用 NextJS TypeScript安装在一个项目上安装了 Reactstrap。 I've copied the Navbar example from the Reactstrap docs .我从Reactstrap 文档中复制了 Navbar 示例。 The navigation shows fine, but on mobile when I click the Navbar toggle, the toggle menu does not open.导航显示正常,但在移动设备上单击导航栏切换按钮时,切换菜单未打开。 In fact, no JavaScript components from Reactstrap worked.事实上,Reactstrap 的 JavaScript 组件都没有工作。 I've tried modals as well, they don't work either.我也尝试过模态,它们也不起作用。

Aside from the JavaScript not working when using the Reactstrap components, the Reactstrap CSS styles work completely fine.除了在使用 Reactstrap 组件时 JavaScript 不工作之外,Reactstrap CSS styles 工作完全正常。

Link to live project codesandbox to test. 链接到实时项目代码和框进行测试。

Code for the Navbar specifically below:导航栏的代码具体如下:

import Link from "next/link";
import {
  NavbarBrand,
  NavbarToggler,
  Collapse,
  NavItem,
  NavLink,
  Nav,
  Navbar,
} from "reactstrap";

import styles from "./Navigation.module.scss";

const Navigation = () => {
  return (
    <div className={styles.nav}>
      <Navbar expand="md">
        <NavbarBrand href="/">Premium Delivery</NavbarBrand>
        <NavbarToggler onClick={function noRefCheck() {}} />
        <Collapse navbar>
          <Nav className="ms-auto" navbar>
            <NavItem>
              <NavLink>
                <Link href="/">Home</Link>
              </NavLink>
            </NavItem>
            <NavItem>
              <NavLink>
                <Link href="/services">Services</Link>
              </NavLink>
            </NavItem>
            <NavItem>
              <NavLink>
                <Link href="/service-fees">Service Fees</Link>
              </NavLink>
            </NavItem>
            <NavItem>
              <NavLink>
                <Link href="/about">About</Link>
              </NavLink>
            </NavItem>
            <NavItem>
              <NavLink>
                <Link href="/contact">Contact</Link>
              </NavLink>
            </NavItem>
          </Nav>
        </Collapse>
      </Navbar>
    </div>
  );
};

export default Navigation;

I don't know why, but their documentation on the Storybook puts the function noRefCheck() as a placeholder.我不知道为什么,但他们在 Storybook 上的文档将function noRefCheck()作为占位符。 You are meant to create your own function to activate the Reactstrap code as described in their Github examples: https://github.com/reactstrap/reactstrap/blob/master/stories/examples/NavbarToggler.js您应该创建自己的 function 以激活 Reactstrap 代码,如他们的 Github 示例中所述: https://github.com/reactstrap/reactstrap/blob/master/stories/examples/NavbarToggler.js

First, import useState so we can use the hook.首先,导入 useState 这样我们就可以使用钩子了。 import { useState } from 'react';

Somewhere before the return, add a hook like const [isOpen, setIsOpen] = useState(false);在返回之前的某处,添加一个钩子,如const [isOpen, setIsOpen] = useState(false);

Next, for the NavbarToggle, add the hook to the Event <NavbarToggler onClick={() => { setIsOpen(!isOpen) }} />接下来,对于 NavbarToggle,将挂钩添加到事件<NavbarToggler onClick={() => { setIsOpen(!isOpen) }} />

Finally, ensure that the Collapse has the correct state最后,确保 Collapse 具有正确的 state

<Collapse isOpen={isOpen} navbar>
Navbar Contents
</Collapse>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM