Skip to content

Commit 6922786

Browse files
committed
Custom hook for the app state!
1 parent afefea4 commit 6922786

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React, { useReducer } from "react";
1+
import React from "react";
22

33
import styles from "./App.module.css";
4-
import { getInitialState, reducer } from "./appState";
4+
import useAppState from "./useAppState";
55

66
function App() {
7-
const [state, dispatch] = useReducer(reducer, null, getInitialState);
7+
const [state, dispatch] = useAppState();
88

99
let content = null;
1010
switch (state.phase) {

src/appState.ts renamed to src/useAppState.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useReducer, type ActionDispatch } from "react";
2+
13
import getRandomWord from "./getRandomWord";
24

35
export type State =
@@ -53,3 +55,7 @@ export function reducer(state: State, action: Action): State {
5355

5456
return state;
5557
}
58+
59+
export default function useAppState(): [State, ActionDispatch<[Action]>] {
60+
return useReducer(reducer, null, getInitialState);
61+
}

0 commit comments

Comments
 (0)