简体   繁体   English

根据用户交互更改react-transition-group退出动画?

[英]Change react-transition-group exit animations depending on user interaction?

I'm fairly new to React and react-transition-group. 我是React和react-transition-group的新手。 I'm making a very simple app that displays a word of the day, and I'm having some trouble with animating my info cards. 我正在创建一个非常简单的应用程序,显示当天的单词,我在设置动画信息卡方面遇到了一些麻烦。

It essentially works like an image carousel. 它基本上像图像轮播一样工作。 If the user clicks a left arrow, the current content slides off to the right and new content slides in from the left. 如果用户单击向左箭头,则当前内容向右滑动,新内容从左侧滑入。 If the user clicks a right arrow, the current content slides off to the left and new content slides in from the right. 如果用户单击向右箭头,则当前内容向左滑动,新内容从右侧滑入。

The problem is, with react-transition-group you have to set your CSS class names when the component renders. 问题是,使用react-transition-group,您必须在组件呈现时设置CSS类名。 But I don't know which direction the component needs to exit until after it's already been rendered. 但我不知道组件在已经渲染之前需要退出的方向。 This means sometimes the components exit the wrong way. 这意味着有时组件以错误的方式退出。

import React from "react";
import words from  '../words.json';
import { TransitionGroup, CSSTransition } from 'react-transition-group';

class TodaysDefinition extends React.Component{

    render(){
        let {day, nextOrPrev} = this.props;
        let {def} = words[day - 1 ];

        //trying to set the classNames prop based on which button was pressed last.
        let transitionClass = "";
        if(nextOrPrev === -1){
            transitionClass = "prev-defs";
        }
        else{transitionClass = "next-defs";}

        return(
            <div className="def-container">
                <TransitionGroup component={null}>
                    <CSSTransition
                    classNames={transitionClass}
                    key={this.props.day}
                    timeout={{enter:300, exit:300}} >
                        <ol className="defs">
                            {def.map((def,idx) => (<li key={idx} >{def}</li>))}
                        </ol>
                    </CSSTransition>
                </TransitionGroup>
            </div>
        )
    }
}

export default TodaysDefinition;

Thanks for your help! 谢谢你的帮助!

As far as I can tell, there's no built-in way to do this via react-transition-group except with their callback functions. 据我所知,除了回调函数之外,还没有通过react-transition-group执行此操作的内置方法。

<CSSTransition
                classNames="next-defs"
                key={this.props.day}
                timeout={{enter:300, exit:300}}
                onEnter={(node) => this.enter(node)}
                onEntering={(node) => this.entering(node)}
                onExit={(node) => this.exit(node)}
                onExiting={(node) => this.exiting(node)} >
                    <ol className="defs">
                        {def.map((def,idx) => (<li key={idx} >{def}</li>))}
                    </ol>
                </CSSTransition>

I have four functions that I use to add classes to the appropriate node at the same times react-transition-group normally does it automatically. 我有四个函数用于在适当的时间将类添加到适当的节点,react-transition-group通常会自动执行。 I keep track of what button was pushed last in state, and then add the appropriate class. 我跟踪状态中最后按下的按钮,然后添加适当的类。

I'm not super happy with the solution, but it works for this project. 我对解决方案并不满意,但它适用于这个项目。 I'm very curious if there's a more react-like way to do this. 我很好奇是否有更像反应的方式来做到这一点。

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

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