【问题标题】:How to apply animation in React JS如何在 React JS 中应用动画
【发布时间】:2019-06-12 06:48:21
【问题描述】:

我正在使用 react-animated-css 库在 React JS 中的状态变化上应用动画。

代码如下:

import ReactDOM from "react-dom";
import React, { Component } from "react";
import { Animated } from "react-animated-css";

const animationIn = "fadeInLeft";
const animationOut = "fadeOutLeft";
const animationDuration = 400; // in ms

const arr = [
  {
    id: 1,
    name: "Test"
  },
  {
    id: 2,
    name: "Test1"
  },
  {
    id: 3,
    name: "Test3"
  },
  {
    id: 4,
    name: "Test4"
  },
  {
    id: 5,
    name: "Test5"
  }
];

class Selection extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selection: []
    };
    this.addSelection = this.addSelection.bind(this);
    this.removeItem = this.removeItem.bind(this);
  }

  addSelection(item) {
    const exists = this.state.selection.find(i => i.id === item.id);
    if (exists === undefined) {
      this.setState({ selection: [...this.state.selection, item] });
    }
  }

  removeItem(item) {
    this.setState({
      selection: this.state.selection.filter(i => i.id !== item.id)
    });
  }

  render() {
    return (
      <div
        style={{
          display: "flex",
          flexDirection: "row",
          justifyContent: "space-between"
        }}
      >
        <div>
          <h2>Choose from the list</h2>
          {arr.map(item => {
            return (
              <div
                key={item.id}
                style={{ marginBottom: 5, cursor: "pointer" }}
                onClick={() => this.addSelection(item)}
              >
                {item.name}
              </div>
            );
          })}
        </div>

        <div>
          <h1>Selection</h1>
          {this.state.selection.length < 1 ? (
            <div>Nothing selected</div>
          ) : (
            this.state.selection.map(l => {
              return (
                <Animated
                  key={l.name}
                  animationIn={animationIn}
                  animationOut={animationOut}
                  animationInDuration={animationDuration}
                  animationOutDuration={animationDuration}
                  isVisible={true}
                >
                  <div key={l.id} style={{ marginBottom: 5 }}>
                    {l.name}
                    <button
                      onClick={() => this.removeItem(l)}
                      style={{ marginLeft: 5, cursor: "pointer" }}
                    >
                      Remove
                    </button>
                  </div>
                </Animated>
              );
            })
          )}
        </div>
      </div>
    );
  }
}

ReactDOM.render(<Selection />, document.getElementById("root"));

当我单击左侧的某个项目并将其添加到状态时它工作正常,但是当我删除它时它不起作用。

Here is the example on sandbox.

知道如何将动画应用于从状态中删除项目吗?

【问题讨论】:

    标签: javascript reactjs react-animated


    【解决方案1】:

    您需要使用动画的props visiblestate 并添加超时。

    addSelection(item) {
        const exists = this.state.selection.find(i => i.id === item.id);
        if (exists === undefined) {
          this.setState({
            selection: [...this.state.selection, item],
            [`visibleAnimate${item.id}`]: true
          });
        }
      }
    
      removeItem(item) {
        this.setState(
          {
            [`visibleAnimate${item.id}`]: false
            // selection: this.state.selection.filter(i => i.id !== item.id)
          },
          () => {
            setTimeout(() => {
              this.setState({
                selection: this.state.selection.filter(i => i.id !== item.id)
              });
            }, 300);
          }
        );
      }
    

    这里是sandbox demo

    【讨论】:

    • 当我删除项目时,它会删除整个列表,而不仅仅是点击的项目。
    • 太好了。谢谢。
    【解决方案2】:

    乍一看,您似乎删除了该项目的动画,这就是它不播放的原因。 如果您将动画环绕在整个选择列表中,从您的 h1 下方开始,它会起作用吗?

    【讨论】:

      【解决方案3】:

      您必须切换isVisible 属性才能看到输出动画。如果组件被卸载,它就不能被动画出来。

      【讨论】:

        猜你喜欢
        • 2021-12-29
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        • 2018-07-15
        • 1970-01-01
        • 1970-01-01
        • 2019-11-04
        • 2014-11-14
        相关资源
        最近更新 更多