javascript - Gatsby 没有在生产中加载 useEffect 函数

标签 javascript reactjs gatsby gsap

我正在使用 Gatsby.js 创建一个网站。
在我的组件中,我在 useEffect 函数中使用 Gsap 创建了动画。

调试时,一切正常。在生产中,useEffect 函数未运行,因此不显示动画。

我该做什么?
有任何想法吗?

感谢您的回答!

我的组件:

import React, { useRef, useEffect } from "react"
import styled from "styled-components"
import gsap from "gsap"
import WhatEver from "../../../static/whatever.svg"
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faArrowDown } from '@fortawesome/free-solid-svg-icons'
import scrollTo from 'gatsby-plugin-smoothscroll';


const HeaderWrapper = styled.header`
  width: 100%;
  height: 100vh;
  min-height: 150px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  background-color: rgb(255, 216, 41);
`

const HeaderButton = styled.button`
  display: block;
  margin: 40px auto;
  border: 2px solid #000000;
`

const HeaderComponent = () => {

  const animWrapper = useRef(null)

  useEffect(() => {

    const [elements] = animWrapper.current.children

    const what = elements.getElementById('What')
    const ever = elements.getElementById('Ever')
    const button = document.getElementById('header-button')
    const icon = document.getElementById('header-icon')

    const whatChildrens = what.children
    const everChildrens = ever.children
    const allChildrens = [...whatChildrens, ...everChildrens]

    gsap.set([...allChildrens, button], { autoAlpha: 0 })

    const timeLine = gsap.timeline({ defaults: { ease: 'power3.inOut' } })

    timeLine
      .to(whatChildrens, { autoAlpha: 1, duration: 0.75 })
      .to(everChildrens, { autoAlpha: 1, stagger: 0.025 })
      .to(button, { autoAlpha: 1 })
  }, [])

  return (
      <HeaderWrapper className="header" id="main-header">
        <div ref={animWrapper} id="header-logo-wrapper">
          <WhatEver style={{width: '100%'}}/>

          <HeaderButton id="header-button" onClick={() => scrollTo('#poznaj-nas')}>
            <FontAwesomeIcon icon={faArrowDown} id="header-icon"/>
          </HeaderButton>
        </div>
      </HeaderWrapper>
  )
}

export default HeaderComponent

最佳答案

我认为正在发生的事情是 gsap 库正在从生产构建中摆脱出来。我会尝试调整你的 webpack 设置以确保它没有被删除,或者像这样包含它:

const gsap = require("gsap")
以我的经验,这阻止了图书馆被树摇晃。

关于javascript - Gatsby 没有在生产中加载 useEffect 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60601817/

相关文章:

javascript - javascript中是否有必要使用嵌套函数?

javascript - 文件被存储几次而不是一次

reactjs - 如何用 jest 进行测试

css - 如何修复破坏特定 Gatsby 页面上的本地样式的 : Gatsby build results in css files being merged,

带有 WordPress 的 Gatsby.js

javascript - 提交事件未捕获提交类型输入

javascript - 没有赋值运算符的变量名周围的大括号是什么意思

javascript - 使用 React Komposer 进行分页

javascript - 我的按钮需要点击两次才能为第一次点击 React 渲染新组件

javascript - 如何根据特定页面有条件地在 Gatsby 中呈现组件?