Guest User

Untitled

a guest
Jun 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. class App extends Component {
  2. constructor(props) {
  3. super(props)
  4. this.state = {
  5. show: false,
  6. shouldRender: false,
  7. class: ''
  8. }
  9. }
  10.  
  11. componentDidUpdate(prevProps, prevState) {
  12. if (!prevState.show && this.state.show) {
  13. this.setState({
  14. shouldRender: true
  15. })
  16. setTimeout(() => {
  17. this.setState({ class: 'showDiv' })
  18. }, 0)
  19. }
  20. if (prevState.show && !this.state.show) {
  21. setTimeout(() => {
  22. this.setState({
  23. shouldRender: false
  24. })
  25. }, 1000)
  26. this.setState({ class: '' })
  27. }
  28. }
  29.  
  30. render() {
  31. return (
  32. <div>
  33. <button
  34. style={{
  35. display: 'block',
  36. border: 'none',
  37. padding: '20px',
  38. background: 'blue',
  39. color: 'white',
  40. fontSize: '20px',
  41. margin: '10px',
  42. cursor: 'pointer'
  43. }}
  44. onClick={() => this.setState({ show: !this.state.show })}
  45. >
  46. Toggle Div
  47. </button>
  48.  
  49. {this.state.shouldRender ? (
  50. <div className={`baseDiv ${this.state.class}`} />
  51. ) : null}
  52.  
  53. <div
  54. style={{
  55. width: '200px',
  56. height: '200px',
  57. background: '#999',
  58. margin: '5px'
  59. }}
  60. />
  61. )
  62. }
  63. }
  64.  
  65. export default App
  66.  
  67. .baseDiv {
  68. width: 200px;
  69. height: 200px;
  70. background: #333;
  71. margin: 5px;
  72. transition: all 1s ease;
  73. transform: translateY(-200%);
  74. }
  75. .showDiv {
  76. transform: translateY(0%);
  77. }
Add Comment
Please, Sign In to add comment