react切换隐藏或显示状态(包含过渡动画)


App.js

import React, { Component } from 'react';
import './style.css'
class App extends Component {
  constructor(props) {
    super(props);
    this.state = { 
      condition:true // 状态(隐藏或显示)
     }
  }
  // 切换隐藏或显示状态
  toggle=()=>{
    this.setState(()=>({
      condition:this.state.condition?false:true
    }))
   
  }
  render() { 
    return ( 
      <div>
        {/* 根据状态决定显示或隐藏的类名 */}
        <div className={this.state.condition?'show' :'hide'}>hello</div>
        <button onClick={this.toggle}>切换</button>
      </div>
     );
  }
}
 
export default App;

css

.show {
  opacity: 1;
  transition: all 1s;
}

.hide {
  opacity: 0;
  transition: all 1s;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM