简体   繁体   English

使用没有jsx的react-spring

[英]using react-spring without jsx

I want to use react-spring for some basic animation. 我想对一些基本动画使用react-spring。 All examples that I can find is based on JSX, which most people obviously are using when developing react. 我可以找到的所有示例均基于JSX,大多数人显然在开发react时都在使用它们。 However in the project I am implementing it, JSX have been turned off, also import is illegal, only require is available. 但是在我正在实施的项目中,JSX已关闭,并且import也是非法的,仅require可用。 This is some es-lint settings to keep code convention. 这是一些es-lint设置,用于保持代码约定。

I have tried following: 我尝试了以下方法:

const createReactClass = require('create-react-class');
const React = require('react');
const Spring = require('react-spring');

const h = React.createElement;

const SectionCollapse = createReactClass({
  render: function () {
    return (
      h(Spring, {
        from: { opacity: 0 },
        to: { opacity: 1 }
      },
      (sp) => {
        return h('div', {}, 'should render');
      }));
  }
});

But I get the error: 但是我得到了错误:

react.development.js:188 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. react.development.js:188警告:React.createElement:类型无效-预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 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 SectionCollapse . 检查SectionCollapse的渲染方法。

This is probably some easy nobrainer that I am missing, 这可能是我想念的一些容易的事,

Looking at the very first example of https://www.react-spring.io/docs/hooks/basics as it looks to me you are trying this from JSX to non JSX. 在我看来,看看https://www.react-spring.io/docs/hooks/basics的第一个示例,您正在尝试从JSX到非JSX。

So the original example is: 因此,原始示例是:

import { useSpring, animated } from 'react-spring'

function App() {
  const props = useSpring({ opacity: 1, from: { opacity: 0 } })
  return <animated.div style={props}>I will fade in</animated.div>
}

Converting your import is basically correct. 转换导入基本上是正确的。

var Spring = require("react-spring");

But I think your h() function properties are lacking some information - That's how I converted the JSX after checking it with https://babeljs.io/repl . 但是我认为您的h()函数属性缺少一些信息-这就是我通过https://babeljs.io/repl检查后转换JSX的方式。

  return h(Spring.animated.div, {
    style: {
    opacity: 1,
    from: {
      opacity: 0
    }
   }
  }, "I will fade in");

Coming back to your example, this should do the trick (untested): 回到您的示例,这应该可以解决问题(未测试):

const createReactClass = require('create-react-class');
const React = require('react');
const Spring = require('react-spring');

const h = React.createElement;

const SectionCollapse = createReactClass({
  render: function () {
    return (h(Spring.animated.div, { style: {
        from: { opacity: 0 },
        to: { opacity: 1 }
       } 
      },
      h('div', {}, 'should render')));
  }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM