如何淡入文本然后退出

Slb*_*box 1 reactjs reactcsstransitiongroup

我正在尝试在文本输入旁边显示通知,以通知用户他们的文本已保存,然后淡出该通知文本.

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

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

我想不出有什么方法可以淡化它,然后出来,这不是荒谬的回旋.

我也在使用Redux,并希望使用它,但这不是必需的.任何意见,将不胜感激.

Fab*_*ltz 5

您可以使用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'));
Run Code Online (Sandbox Code Playgroud)
span {
  transition: 300ms all ease;
  opacity: 0;
  will-change: opacity;
}

.show {
  opacity: 1;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)