【问题标题】:react components state null gatsby反应组件状态为空盖茨比
【发布时间】:2021-01-14 12:10:09
【问题描述】:

我遇到的问题是组件中的状态为 null 并且导航菜单中的滚动不起作用。我认为问题出在 Scrollto 组件中,但仍然不知道如何解决它。这是一个导航列表,应该从部分列表中获取数据,并在单击所选部分后平滑滚动。 如果有更好的方法来实现我的代码,我会很高兴听到。

导航菜单组件

import React, { useState } from "react"
import scrollTo from "gatsby-plugin-smoothscroll"
import Scrollspy from "react-scrollspy"
import { useSiteMetadata } from "../hooks/use-site-metadata"

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faBars } from "@fortawesome/free-solid-svg-icons"

export default function () {
  const [isVisible, setVisibility] = useState(false)

  const { menuItems } = useSiteMetadata()

  let showStyle = null
  if (isVisible) {
    showStyle = { transform: "scaleY(1)" }
  } else {
    showStyle = null
  }

  return (
    <nav className="nav">
      <button
        className="btn-tog"
        id="toggle-btn"
        href="/#"
        title="Menu"
        onClick={() => setVisibility(!isVisible)}
      >
        <FontAwesomeIcon icon={faBars} />
      </button>

      <Scrollspy
        className=" nav-ul navList list flex"
        style={showStyle}
        items={menuItems.map(a => a.path)}
        currentClassName="current"
        offset={-100}
      >
        {menuItems.map((value, index) => {
          return (
            <li className="mr3" key={index}>
              <button
                onClick={() => 
                  {
                  scrollTo("#" + value.path)
                  setVisibility(0)
                }}
              >
                {value.label}
              </button>
            </li>
          )
        })}
      </Scrollspy>
    </nav>
  )
}

使用站点元数据

import { useStaticQuery, graphql } from "gatsby"
export const useSiteMetadata = () => {
  const { site } = useStaticQuery(
    graphql`
      query MyQuery {
        site {
          siteMetadata {
            title
            description
            author {
              name
            }
            sections
            favicon
            logo
            menuItems {
              path
              label
            }
          }
        }
      }
    `
  )
  return site.siteMetadata
}

存储数据

module.exports = {
  siteMetadata: {  
    /* Choose and arrange the sections to be displayed on the landing page */
    sections: [`hero`, `about`, `mappingItems`, `contact`],

    /* Configure the navigation menu */
    menuItems: [
      { path: "hero", label: "Home" },
      { path: "about", label: "About" },
      { path: "mappingItems", label: "MappingItems" },
      { path: "contact", label: "Contact" },
    ],

index.js

import React from "react"
import Layout from "../components/layout"
import Hero from "./Hero"
import About from "./about"
import Contact from "./contact"
import MappingItems from "./mappingItems"




import { useSiteMetadata } from "../hooks/use-site-metadata"

export default () => {
  const { sections } = useSiteMetadata()
  const availableSections = {
    hero: Hero,
    about: About,
    mappingItems: MappingItems,
    contact: Contact,
  }

  return (
    <>
      <Layout>
        {sections.map(section => {
          let Tagname = availableSections[section]
          return <Tagname />
        })}
      </Layout>
    </>
  )
}

【问题讨论】:

  • 很难回答你的问题(无法通过复制粘贴代码来测试页面是否滚动)。还有与CSS相关的错误截图。
  • @EzraSiton tnx 为您的回复。 codesandbox.io/s/github/desmukh/gatsby-starter-woo?file=/src/… - 这里有启动器,我得到了与制作导航相同的代码的逻辑。没有 css 它仍然可以工作,所以它不是 css。

标签: reactjs scroll state gatsby


【解决方案1】:

问题在于它应该位于具有相同 id 的 availableSections 包装部分中的部分(组件)中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2021-03-14
    • 1970-01-01
    • 2021-06-15
    • 2022-01-25
    相关资源
    最近更新 更多