Skip to content

Commit 928956c

Browse files
committed
Custom hook for loading the data!
1 parent 63e5da0 commit 928956c

3 files changed

Lines changed: 24 additions & 16 deletions

File tree

src/App.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
import React, { useEffect } from "react";
1+
import React from "react";
22

33
import styles from "./App.module.css";
4-
import useAppState from "./useAppState";
5-
import normalizeString from "./util/normalizeString";
4+
import useAppState from "./hooks/useAppState";
5+
import useLoadData from "./hooks/useLoadData";
66
import pluralize from "./util/pluralize";
77

88
function App() {
99
const [state, dispatch] = useAppState();
10-
11-
useEffect(() => {
12-
fetch("fruits.txt")
13-
.then((response) => response.text())
14-
.then((text) => {
15-
dispatch({
16-
type: "load-data",
17-
wordPack: text.split("\n").map(normalizeString).filter(Boolean),
18-
});
19-
});
20-
}, [dispatch]);
10+
useLoadData(dispatch);
2111

2212
switch (state.phase) {
2313
case "pre-game": {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useReducer, type Dispatch } from "react";
22

3-
import getRandomElement from "./util/getRandomElement";
4-
import normalizeString from "./util/normalizeString";
3+
import getRandomElement from "../util/getRandomElement";
4+
import normalizeString from "../util/normalizeString";
55

66
export type State = Readonly<
77
| {

src/hooks/useLoadData.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { type Dispatch, useEffect } from "react";
2+
3+
import normalizeString from "../util/normalizeString";
4+
5+
export default function useLoadData(
6+
dispatch: Dispatch<{ type: "load-data"; wordPack: readonly string[] }>,
7+
) {
8+
useEffect(() => {
9+
fetch("fruits.txt")
10+
.then((response) => response.text())
11+
.then((text) => {
12+
dispatch({
13+
type: "load-data",
14+
wordPack: text.split("\n").map(normalizeString).filter(Boolean),
15+
});
16+
});
17+
}, [dispatch]);
18+
}

0 commit comments

Comments
 (0)