Skip to content

Commit a2e3dda

Browse files
committed
[Game] removed DEFAULT_STATE
It was a bit concerning having to deal with worrying about mutations, so I decided to just write out the stuff two-ish times. My concerns were along the lines of, e.g. microsoft/TypeScript#10725 , and as a side note, it's a bit annoying that I have to worry about mutating prevState in this.setState.
1 parent 5535b52 commit a2e3dda

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/Game/Game.tsx

+18-12
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ interface State {
6969
playerToggleKey: number,
7070
};
7171

72-
const DEFAULT_STATE: State = {
72+
interface Props extends WithStyles<typeof styles> { };
73+
74+
class Game extends React.Component<Props, State> {
75+
constructor(props: Props) {
76+
super(props);
77+
78+
this.state = {
7379
playerType: "player",
7480
turn: "red",
7581
winner: "",
@@ -79,15 +85,7 @@ const DEFAULT_STATE: State = {
7985
},
8086
groupedWords: [], // as GroupedWord[],
8187
playerToggleKey: 0
82-
};
83-
84-
interface Props extends WithStyles<typeof styles> { };
85-
86-
class Game extends React.Component<Props, State> {
87-
constructor(props: Props) {
88-
super(props);
89-
90-
this.state = DEFAULT_STATE;
88+
};
9189

9290
this.togglePlayerType = this.togglePlayerType.bind(this);
9391
this.endTurnHandler = this.endTurnHandler.bind(this);
@@ -100,8 +98,16 @@ class Game extends React.Component<Props, State> {
10098
}
10199

102100
createNewGame() {
103-
this.setState({ ...DEFAULT_STATE });
104-
this.setState({ playerToggleKey: Math.random() }) // I just need this to change somehow.
101+
this.setState({
102+
playerType: "player",
103+
turn: "red",
104+
winner: "",
105+
remaining: {
106+
red: 9,
107+
blue: 8
108+
},
109+
playerToggleKey: Math.random()
110+
});
105111

106112
shuffle(WORDBANK);
107113
const gameWords: string[] = WORDBANK.slice(0, 25);

0 commit comments

Comments
 (0)