简体   繁体   English

未找到 GSAP 目标未定义

[英]GSAP target undefined not found

With useSate I toggle between two paragraphs of text from french to english.使用 useSate,我可以在从法语到英语的两段文本之间切换。 It works fine but I want to set up a nicer transition with GSAP.它工作正常,但我想与 GSAP 建立更好的过渡。

My configuration doesn't work because the gsap target is null or undefined.我的配置不起作用,因为 gsap 目标为空或未定义。 Can you help me to debug that problem?你能帮我调试这个问题吗?

The paragraph component with the two buttons :带有两个按钮的段落组件:

      import React, { useRef, useState } from "react";
import { withRouter } from "react-router-dom";
import gsap from 'gsap';

const LanguageDesc = (props) => {

    const [state, setState] = useState({
        isActive: false,
        languageName: 'English'
    });

    let english = useRef(null);
    let french = useRef(null);

    const handleLanguage = () => {
        if (state.isActive === false) {
            setState({
                isActive: true,
                languageName: 'French'
            })
            gsap.from(english.current, {
                duration: 5,
                opacity: 0,
            })
        } else if (state.isActive === true) {
            setState({
                isActive: false,
                languageName: 'English'
            })
            gsap.from(french.current, {
                duration: 1,
                opacity: 0,
            });
        } else if (state.isActive === false) {
            setState({
                isActive: !state.isActive,
                languageName: 'French'
            })
            gsap.from(english.current, {
                duration: 5,
                opacity: 0,
            })
        }
    };


    return (
        <div>
            {state.isActive ?
                <p ref={english} className="eng-text">{props.state.desc_en}</p>
                : null}
            {state.isActive ?
                null
                : <p ref={french} className="fr-text">{props.state.desc_fr}</p>}
            <button className="btn-language" onClick={handleLanguage} > {state.languageName} text</button>
        </div >
    )

};
export default withRouter(LanguageDesc);

安慰

The problem is ref={el => (english = el)} and the same usage for the french ref.问题是ref={el => (english = el)}french ref 的用法相同。

Refs returned by useRef are references to a single object that is held in memory across the lifetime of the component. useRef返回的useRef是对单个对象的引用,该对象在组件的整个生命周期内都保存在内存中。 You must read from and write to the ref using the current property.您必须使用current属性读取和写入 ref。

Assigning english to the value of el does not change the value of the english ref, it replaces the ref with el .english分配给el的值并不会改变 english ref 的值,而是ref替换el You then access the current property on the element, which (since it is not a ref) will always be undefined .然后您访问元素上的current属性,该属性(因为它不是引用)将始终为undefined

english is already a ref, so you don't need to handle the assignment yourself. english已经是 ref,所以你不需要自己处理作业。 Just do ref={english} and React will handle the rest.只需执行ref={english} ,React 将处理其余的。

Update更新

The issue remains because your code still uses the refs as elements when they are null .问题仍然存在,因为当它们为null时,您的代码仍然使用 refs 作为元素。

When isActive is false, there is no english element.isActive为 false 时,没有english元素。 When true, no french element.当为真时,没有french元素。 Yet each event handler depends on both elements being present.然而,每个事件处理程序都依赖于存在的两个元素。

Removing the elements entirely increases the complexity of animating the in/out transitions.完全删除元素会增加输入/输出转换动画的复杂性。 Maybe instead you can use isActive to determine whether to display each p element ( display: none; ), but always render both.也许您可以使用isActive来确定是否显示每个p元素( display: none; ),但始终同时渲染。

() => {
  const handleLanguage = () => {
    if (isActive) {
      gsap
        .from(french.current, {
          duration: 1,
          opacity: 0,
        })
        .eventCallback('onComplete', () => {
          setState({
            isActive: false,
            languageName: 'English',
          });
        });
    } else {
      gsap
        .from(english.current, {
          duration: 5,
          opacity: 0,
        })
        .eventCallback('onComplete', () => {
          setState({
            isActive: true,
            languageName: 'French',
          });
        });
    }
  };

  return (
    <div>
      <p
        ref={english}
        style={{ display: isActive ? 'none' : 'initial' }}
        className="eng-text"
      >
        {desc_en}
      </p>
      <p
        ref={french}
        style={{ display: isActive ? 'initial' : 'none' }}
        className="fr-text"
      >
        {desc_fr}
      </p>
      <button className="btn-language" onClick={handleLanguage}>
        {' '}
        {state.languageName} text
      </button>
    </div>
  );
};

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

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