ReactStrap Navbar切换未在NextJS打字条项目中切换

发布于 2025-01-17 14:21:48 字数 2095 浏览 0 评论 0 原文

我已经安装了 reactstrap> reactstrap 在项目上使用NextJS上的项目打字稿安装。我已经从

除了使用ReactStrap组件时的JavaScript不起作用外,ReactStrap CSS样式还可以正常工作。

链接到实时项目codescodbox live project topport box live test。 /a>

代码的Navbar专门以下:

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've installed Reactstrap on a project using the NextJS TypeScript install. I've copied the Navbar example from the Reactstrap docs. 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. 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.

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2025-01-24 14:21:48

我不知道为什么,但是他们在故事书上的文档将函数norefcheck()作为占位符。您本来可以创建自己的函数来激活ReactStrap代码,如其GitHub示例中所述:

首先,导入Usestate,以便我们可以使用钩子。
import {usestate}来自'react'';

在返回之前的某个地方,添加钩子
const [Isopen,stosopen] = usestate(false);

接下来,对于navbartoggle,将钩子添加到事件中
&lt; navbartoggler onClick = {()=&gt; {setIsopen(!isopen)}}} /&gt; < /code>

最后,确保崩溃具有正确的状态

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

I don't know why, but their documentation on the Storybook puts the function noRefCheck() as a placeholder. 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

First, import useState so we can use the hook.
import { useState } from 'react';

Somewhere before the return, add a hook like
const [isOpen, setIsOpen] = useState(false);

Next, for the NavbarToggle, add the hook to the Event
<NavbarToggler onClick={() => { setIsOpen(!isOpen) }} />

Finally, ensure that the Collapse has the correct state

<Collapse isOpen={isOpen} navbar>
Navbar Contents
</Collapse>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文