Browse Source

Use pure component for cells

Arnaud Vergnet 4 years ago
parent
commit
7b45841c30
3 changed files with 34 additions and 14 deletions
  1. 1
    1
      screens/Tetris/GameLogic.js
  2. 31
    12
      screens/Tetris/components/Cell.js
  3. 2
    1
      screens/Tetris/components/Grid.js

+ 1
- 1
screens/Tetris/GameLogic.js View File

@@ -25,7 +25,7 @@ export default class GameLogic {
25 25
         this.height = height;
26 26
         this.width = width;
27 27
         this.gameRunning = false;
28
-        this.gameTick = 250;
28
+        this.gameTick = 1000;
29 29
     }
30 30
 
31 31
     getHeight(): number {

+ 31
- 12
screens/Tetris/components/Cell.js View File

@@ -4,18 +4,37 @@ import * as React from 'react';
4 4
 import {View} from 'react-native';
5 5
 import {withTheme} from 'react-native-paper';
6 6
 
7
-function Cell(props) {
8
-    const colors = props.theme.colors;
9
-    return (
10
-        <View style={{
11
-            flex: 1,
12
-            backgroundColor: props.color,
13
-            borderColor: props.isEmpty ? props.color : "#393939",
14
-            borderStyle: 'solid',
15
-            borderWidth: 1,
16
-            aspectRatio: 1,
17
-        }}/>
18
-    );
7
+type Props = {
8
+    color: string,
9
+    isEmpty: boolean,
10
+    id: string,
11
+}
12
+
13
+class Cell extends React.PureComponent<Props> {
14
+
15
+    colors: Object;
16
+
17
+    constructor(props) {
18
+        super(props);
19
+        this.colors = props.theme.colors;
20
+    }
21
+
22
+    render() {
23
+        return (
24
+            <View
25
+                style={{
26
+                    flex: 1,
27
+                    backgroundColor: this.props.color,
28
+                    borderColor: this.props.isEmpty ? this.props.color : "#393939",
29
+                    borderStyle: 'solid',
30
+                    borderWidth: 1,
31
+                    aspectRatio: 1,
32
+                }}
33
+            />
34
+        );
35
+    }
36
+
37
+
19 38
 }
20 39
 
21 40
 export default withTheme(Cell);

+ 2
- 1
screens/Tetris/components/Grid.js View File

@@ -25,7 +25,8 @@ class Grid extends React.Component<Props>{
25 25
         let cells = [];
26 26
         for (let i = 0; i < this.props.width; i++) {
27 27
             let cell = this.props.grid[rowNumber][i];
28
-            cells.push(<Cell color={cell.color} isEmpty={cell.isEmpty}/>);
28
+            let key = rowNumber + ':' + i;
29
+            cells.push(<Cell color={cell.color} isEmpty={cell.isEmpty} id={key}/>);
29 30
         }
30 31
         return(
31 32
             <View style={{

Loading…
Cancel
Save