【问题标题】:Gatsby - IntersectionObserver is not definedGatsby - IntersectionObserver 未定义
【发布时间】:2019-12-20 11:13:14
【问题描述】:

我正在尝试构建我的 gatsby 项目,但由于 IntersectionObserver 未被识别,我无法构建。我在 InView 组件中使用 intersectionObserver:

import React, { useRef, useState, useEffect } from 'react'


const InView = ({ children }) => {
    const [boundingClientY, setBoundingClientY] = useState(null)
    const [direction, setDirection] = useState(null)
    const [element, setElement] = useState(null)
    const [inView, setInView] = useState(false)



    const observer = useRef(new IntersectionObserver((entries) => {
        const first = entries[0]
        const { boundingClientRect } = first
        first.isIntersecting && setInView(true)
        !first.isIntersecting && setInView(false)
        boundingClientRect.y > boundingClientY && setDirection('down')
        boundingClientRect.y < boundingClientY && setDirection('up')
        boundingClientY && setBoundingClientY(first.boundingClientRect.y)

    }))


    useEffect(() => {
        const currentElement = element
        const currentObserver = observer.current
        currentElement && currentObserver.observe(currentElement)
        // console.log(currentObserver)
        return () => {
            currentElement && currentObserver.unobserve(currentElement)
        };
    }, [element])

    const styles = {
        opacity: inView ? 1 : 0,
        transform: `
            translateY(${!inView ?
                direction === 'up' ? '-20px' : '20px'
                : 0})
            rotateY(${!inView ? '35deg' : 0})
            scale(${inView ? 1 : 0.9})
            `,
        transition: 'all 0.4s ease-out 0.2s'
    }

    return (
        <div ref={setElement} style={styles}>

            {children}

        </div>
    )
}

export default InView

我有一个根元素的包装器来启用全局状态,并尝试在 gatsby-browser.js 中导入 polyfill:

import React from 'react'
import GlobalContextProvider from './src/components/context/globalContextProvider'

export const wrapRootElement = ({ element }) => {
    return (
        <GlobalContextProvider>
            {element}
        </GlobalContextProvider>
    )
}

export const onClientEntry = async () => {
    if (typeof IntersectionObserver === `undefined`) {
        await import(`intersection-observer`);
    }
}

【问题讨论】:

    标签: reactjs gatsby polyfills intersection-observer


    【解决方案1】:

    这是构建错误,对吧($ gatsby build)?如果是这种情况,这与浏览器支持无关。

    事实上,IntersectionObserver 是一个浏览器 API,您不应该在服务器端渲染期间使用浏览器 API。相反,您尝试在组件安装后使用它们。要解决这个问题,请在 useEffect() 中初始化您的观察者,而不是像您目前所做的那样使用 useRef()

    ...
    const observer = useRef();
    
    useEffect(() => {
      observer.current = new IntersectionObserver({ ... });
    }, []); // do this only once, on mount
    ...
    

    【讨论】:

      【解决方案2】:

      声明一个 let 变量 = null。这也适用于 NextJS

      ...
      let observer = null
      
      useEffect(()=> {
      observer = new IntersectionObserver(callback,optional);
      },[])
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 1970-01-01
        • 2020-10-22
        • 1970-01-01
        • 2021-06-27
        • 2021-08-04
        相关资源
        最近更新 更多