简体   繁体   English

简单的 react-spring 组件在 gatsby 中不起作用 - 元素类型无效:预期为字符串或类/函数但得到:未定义

[英]Simple react-spring component not working in gatsby - Element type is invalid: expected a string or a class/function but got: undefined

Trying out react-spring, using a simple tutorial.使用简单的教程尝试 react-spring。 However, it's not working for me.但是,它对我不起作用。 Here's my code:这是我的代码:

import React, { Component } from "react"
import { Spring } from "react-spring"
export default class Box extends Component {
  render() {
    return (
      <Spring
        from={{ opacity: 0, marginTop: -1000 }}
        to={{ opacity: 1, marginTop: 0 }}
      >
        {props => (
          <div style={props}>
            <p>Welcome to your new Gatsby site.</p>
            <p>Now go build something great.</p>
          </div>
        )}
      </Spring>
    )
  }
}

and here's the error这是错误

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义。 You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

Check the render method of Box .检查Box的渲染方法。

I'm using react-spring in gatsby's default starter, and am simply importing my Box component like so:我在 gatsby 的默认启动器中使用 react-spring,并且只是像这样导入我的 Box 组件:

import React from "react"
import Layout from "../components/layout"
import Box from "../components/box"

const IndexPage = () => (
  <Layout>
    <Box />
  </Layout>
)

export default IndexPage

The changelog says that in version 9.0 they changed how to import things and import { Spring, useSpring } from 'react-spring' should work and should be used.更改日志说,在 9.0 版中,他们更改了导入内容的方式,并且import { Spring, useSpring } from 'react-spring'应该可以工作并且应该使用。

I found on their GitHub issues the solution that worked for me. 我在他们的 GitHub 问题上找到了对我有用的解决方案。

Put: '^react-spring$': '<rootDir>/node_modules/react-spring/web.cjs', in jest.config.js把: '^react-spring$': '<rootDir>/node_modules/react-spring/web.cjs', in jest.config.js

In my case that was a submodule in monorepo so I had to jump up two directories to reach that library: '^react-spring$': '<rootDir>/../../node_modules/react-spring/web.cjs'在我的例子中,这是 monorepo 中的一个子模块,所以我必须跳上两个目录才能到达该库: '^react-spring$': '<rootDir>/../../node_modules/react-spring/web.cjs'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 元素类型无效:应为字符串或类/函数,但得到:未定义 - Element type is invalid: expected a string or a class/function but got: undefined 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义的组件问题 - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined component issue React Native - “元素类型无效:需要字符串或类/函数但未定义” - 导入自定义库? - React Native - “Element type is invalid: expected a string or a class/function but got undefined” - importing custom library? 反应错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - React Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React Native 错误:元素类型无效:应为字符串或类/函数,但得到:未定义 - React Native error! Element type is invalid: expected a string or a class/function but got: undefined 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义的 React - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React 世博反应本机元素类型无效:预期为字符串或类/函数,但得到:未定义 - Expo react native Element type is invalid: expected a string or a class/function but got: undefined React - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - React - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 错误:缩小的 React 错误 #130:元素类型无效:预期为字符串或类/函数,但得到:未定义 - Error: Minified React error #130: Element type is invalid: expected a string or a class/function but got: undefined React Native 错误:元素类型无效:应为字符串或类/函数(对于复合组件)但得到:未定义 - React Native Error: Element type is invalid: expected a string or a class/function (for composite components) but got: undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM