reactjs - 使用 jarallax javascript 库时 Gatsbyjs 构建错误

标签 reactjs gatsby

使用 Gatsby v2。

我想使用 jarallax javascript 库 ( https://free.nkdev.info/jarallax/ ) 以便在我的索引页上添加一些视差效果。它不是一个 react 组件,所以为了避免任何不兼容,我只是将它包装在一个组件中,例如:

import React from "react"
import { jarallax } from "jarallax"

class Jarallax extends React.Component {
  componentDidMount() {
    jarallax(document.querySelectorAll(".jarallax"), { speed: 0.2 })
  }
  componentWillUnmount() {
    jarallax(document.querySelectorAll(".jarallax"), "destroy")
  }
  render() {
    return null
  }
}

export default Jarallax

然后在 IndexPage 中,当我想使用它时,我只需导入该组件并简单地使用它,如 <Jarallax /> 它适用于开发

我也有这个gatsby-node.js文件遵循 Gatsby 文档:

exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
  if (stage === "build-html") {
    actions.setWebpackConfig({
      module: {
        rules: [
          {
            test: /flickity/,
            use: loaders.null(),
          },
          {
            test: /jarallax/, // <--
            use: loaders.null(),
          },
        ],
      },
    })
  }
}

现在,如果我尝试运行 gatsby build我面临这个错误( https://pastebin.com/CXVaQA5f ),但由于我对 react 和 Gatsby 还很陌生,所以现在不太确定下一步是什么。

最佳答案

我解决了这个问题,替换 <Jarallax />对于 {typeof document !== "undefined" && <Jarallax />}在我的IndexPage

这对我有帮助:https://github.com/gatsbyjs/gatsby/issues/9038#issuecomment-432548071

关于reactjs - 使用 jarallax javascript 库时 Gatsbyjs 构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55143356/

相关文章:

javascript - React中setState增量和每次增量1有什么区别

css - 如何在Gatsby中禁用内联CSS?

通过相对路径访问文件的 GraphQL 语法

gatsby - Gatsby 中 Chakra-ui 的扩展主题

properties - 确保数组属性的每个元素都符合 React 中的自定义形状

css - 如何将字符串 + javascript 组合为类名?

javascript - 如何在 React JS 应用程序中使用 webpack 在 img 标签中提供静态图像路径

使用 gatsby-plugin-react-i18next 时 Gatsby 找不到客户端路由

reactjs - GatsbyJS 从 Restful API 获取数据

reactjs - 自动重新获取不起作用 - RTK 查询