1
- import React from 'react' ;
1
+ import { useState , useEffect } from 'react' ;
2
2
3
3
// this can get refactored into something better
4
4
const mapUpToSum = ( num , fn ) => {
@@ -30,18 +30,16 @@ const generateCols = (n, m, classPattern = [''], randomize = false) => {
30
30
) ) ;
31
31
} ;
32
32
33
- export default class Banner extends React . Component {
34
- constructor ( props ) {
35
- super ( props ) ;
36
- this . timer = null ;
37
- this . color = 0 ;
38
- this . state = { randomize : false } ;
39
- }
33
+ const Banner = ( props ) => {
34
+ const [ randomize , setRandomize ] = useState ( false ) ;
35
+ const [ color , setColor ] = useState ( 0 ) ;
36
+ let timer ;
37
+
38
+ useEffect ( ( ) => {
39
+ setRandomize ( true ) ;
40
40
41
- componentDidMount ( ) {
42
- this . setState ( { randomize : true } ) ;
43
41
const committees = [ 'acm' ] ;
44
- if ( ! this . props . decorative )
42
+ if ( ! props . decorative ) {
45
43
committees . push (
46
44
'studio' ,
47
45
'icpc' ,
@@ -52,78 +50,78 @@ export default class Banner extends React.Component {
52
50
'ai' ,
53
51
'hack' ,
54
52
) ;
53
+ }
54
+
55
55
const el = document . querySelector ( '.banner' ) ;
56
- this . timer = setInterval ( ( ) => {
57
- el . classList . remove ( committees [ this . color ] ) ;
58
- this . color = ( this . color + 1 ) % committees . length ;
59
- el . classList . add ( committees [ this . color ] ) ;
60
- this . forceUpdate ( ) ;
56
+ timer = setInterval ( ( ) => {
57
+ el . classList . remove ( committees [ color ] ) ;
58
+ setColor ( ( prevColor ) => ( prevColor + 1 ) % committees . length ) ;
59
+ el . classList . add ( committees [ color ] ) ;
61
60
} , 4000 ) ;
62
- }
63
61
64
- componentWillUnmount ( ) {
65
- clearInterval ( this . timer ) ;
66
- this . timer = null ;
67
- }
62
+ return ( ) => {
63
+ clearInterval ( timer ) ; // Cleanup on unmount
64
+ } ;
65
+ } , [ color , props . decorative ] ) ; // Re-run when color or decorative props change
68
66
69
- render ( ) {
70
- const decorative = this . props . decorative || false ;
67
+ const decorative = props . decorative || false ;
68
+ const sideCols = props . sideCols || ( decorative ? 12 : 7 ) ;
69
+ const height = props . height || ( decorative ? 2 : 7 ) ;
70
+ const width = props . width || 5 ;
71
71
72
- const sideCols = this . props . sideCols || ( decorative ? 12 : 7 ) ;
73
- const height = this . props . height || ( decorative ? 2 : 7 ) ;
74
- const width = this . props . width || 5 ;
75
- return (
76
- < div className = { `banner ${ decorative ? 'decorative' : '' } ` } >
77
- < div className = "square-col-container" >
78
- { ! decorative &&
79
- generateCols ( 1 , height , [
80
- '' ,
81
- '' ,
82
- '' ,
83
- '' ,
84
- 'white' ,
85
- 'white' ,
86
- 'white' ,
87
- ] ) }
88
- { ! decorative &&
89
- generateCols ( 1 , height , [ '' , '' , '' , '' , '' , 'white' , 'white' ] ) }
90
- { generateCols ( 1 , height , [ '' , '' , '' , '' , '' , '' , 'white' ] ) }
91
- { generateCols ( sideCols , height , undefined , this . state . randomize ) }
92
- { ! decorative &&
93
- generateCols ( 1 , height , [ '' , '' , 'white' , 'white' , '' , '' , '' ] ) }
94
- { ! decorative &&
95
- generateCols ( width , height , [
96
- '' ,
97
- '' ,
98
- 'white' ,
99
- 'white' ,
100
- 'white' ,
101
- '' ,
102
- '' ,
103
- ] ) }
104
- { ! decorative &&
105
- generateCols ( 1 , height , [ '' , '' , '' , 'white' , 'white' , '' , '' ] ) }
106
- { generateCols ( sideCols , height , undefined , this . state . randomize ) }
107
- { generateCols ( 1 , height , [ 'white' , '' , '' , '' , '' , '' , '' ] ) }
108
- { ! decorative &&
109
- generateCols ( 1 , height , [ 'white' , 'white' , '' , '' , '' , '' , '' ] ) }
110
- { ! decorative &&
111
- generateCols ( 1 , height , [
112
- 'white' ,
113
- 'white' ,
114
- 'white' ,
115
- '' ,
116
- '' ,
117
- '' ,
118
- '' ,
119
- ] ) }
120
- </ div >
121
- { ! decorative && (
122
- < div className = "title" >
123
- < h1 > code the future.</ h1 >
124
- </ div >
125
- ) }
72
+ return (
73
+ < div className = { `banner ${ decorative ? 'decorative' : '' } ` } >
74
+ < div className = "square-col-container" >
75
+ { ! decorative &&
76
+ generateCols ( 1 , height , [
77
+ '' ,
78
+ '' ,
79
+ '' ,
80
+ '' ,
81
+ 'white' ,
82
+ 'white' ,
83
+ 'white' ,
84
+ ] ) }
85
+ { ! decorative &&
86
+ generateCols ( 1 , height , [ '' , '' , '' , '' , '' , 'white' , 'white' ] ) }
87
+ { generateCols ( 1 , height , [ '' , '' , '' , '' , '' , '' , 'white' ] ) }
88
+ { generateCols ( sideCols , height , undefined , randomize ) }
89
+ { ! decorative &&
90
+ generateCols ( 1 , height , [ '' , '' , 'white' , 'white' , '' , '' , '' ] ) }
91
+ { ! decorative &&
92
+ generateCols ( width , height , [
93
+ '' ,
94
+ '' ,
95
+ 'white' ,
96
+ 'white' ,
97
+ 'white' ,
98
+ '' ,
99
+ '' ,
100
+ ] ) }
101
+ { ! decorative &&
102
+ generateCols ( 1 , height , [ '' , '' , '' , 'white' , 'white' , '' , '' ] ) }
103
+ { generateCols ( sideCols , height , undefined , randomize ) }
104
+ { generateCols ( 1 , height , [ 'white' , '' , '' , '' , '' , '' , '' ] ) }
105
+ { ! decorative &&
106
+ generateCols ( 1 , height , [ 'white' , 'white' , '' , '' , '' , '' , '' ] ) }
107
+ { ! decorative &&
108
+ generateCols ( 1 , height , [
109
+ 'white' ,
110
+ 'white' ,
111
+ 'white' ,
112
+ '' ,
113
+ '' ,
114
+ '' ,
115
+ '' ,
116
+ ] ) }
126
117
</ div >
127
- ) ;
128
- }
129
- }
118
+ { ! decorative && (
119
+ < div className = "title" >
120
+ < h1 > code the future.</ h1 >
121
+ </ div >
122
+ ) }
123
+ </ div >
124
+ ) ;
125
+ } ;
126
+
127
+ export default Banner ;
0 commit comments