reactjs - 如何淡入淡出文本

我正在尝试让通知接下来出现在文本输入中以通知用户他们的文本已保存,然后淡出该通知文本。

基本上,当提交相应的文本输入时,我会尝试复制此操作。

$(this).fadeOut().next().fadeIn();

我想不出有什么方法可以让它淡入淡出,这不是荒谬的迂回。

我也在使用 Redux,并且想使用它,但这不是必需的。任何建议将不胜感激。

最佳答案

您可以使用 CSS 来解决这个问题。这是一个非常简单的例子(不使用 redux)。使用 JS 触发,CSS 样式。

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      show: false
    };
    this.showNotification = this.showNotification.bind(this);
  }
  showNotification() {
    // You can use redux for this.
    this.setState({
      show: true,
    });
    setTimeout(() => {
      this.setState({
        show: false,
      });
    }, 1000);
  }
  render() {
    return (
      <div>
        <button onClick={this.showNotification}>Save</button>
        <Notification show={this.state.show} />
      </div>
    );
  }

}

class Notification extends React.Component {
  render() {
    return <span className={this.props.show ? 'show' : ''}>Saved!</span>
  }
}

ReactDOM.render(<App/>, document.getElementById('View'));
span {
  transition: 300ms all ease;
  opacity: 0;
  will-change: opacity;
}

.show {
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="View"></div>

https://stackoverflow.com/questions/40441239/

相关文章:

angular - 在 Angular2 中,您如何检测过滤器/管道未返回任何结果

php - 使用干预将图像的 DPI 从 72 更改为 300

php - 如何使用 laravel 5.2 获取 Blade 文件(HTML)中的最后一部分 ur

python - django 模板中的多个加载标签

performance - AWS 资源共享

math - 围绕一个点旋转 Vector2

angular - 如何检测下拉列表元素的变化?

asp.net - 时间的 DataFormatString (ASP.Net)

spring-boot - 我如何在 Spring Boot-1.4.1-RELEASE 中制作 i

git - 如何直接从 Google Cloud Source Repository 部署到 Goo