Skip to content

Commit 47a57c8

Browse files
committed
Changed 47 setup
1 parent 3208fb9 commit 47a57c8

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

src/06-advanced-hooks/47-discriminated-unions-in-usestate.problem.tsx

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
11
import { appendVideoToDomAndPlay, fetchVideo } from "fake-external-lib";
22
import { useEffect, useState } from "react";
33

4-
/**
5-
* Here, we're doing the same trick as before - using a state
6-
* variable to track the status of the video loading.
7-
*
8-
* But this time, we're storing it an object, with an extra
9-
* property for the error.
10-
*
11-
* This could be improved - we should only be allowed to specify
12-
* the error property when the status is "error".
13-
*
14-
* 1. Change the State interface so that the error property is only
15-
* allowed when the status is "error".
16-
*/
17-
18-
type Status = "loading" | "loaded" | "error";
19-
20-
interface State {
21-
status: Status;
22-
error?: Error;
23-
}
24-
254
export const useLoadAsyncVideo = (src: string) => {
26-
const [state, setState] = useState<State>({
5+
const [state, setState] = useState({
276
status: "loading",
287
});
298

@@ -62,4 +41,8 @@ export const useLoadAsyncVideo = (src: string) => {
6241

6342
// @ts-expect-error
6443
setState({ status: "loaded", error: new Error("error") });
44+
45+
if (state.status === "error") {
46+
console.error(state.error);
47+
}
6548
};

src/06-advanced-hooks/47-discriminated-unions-in-usestate.solution.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ export const useLoadAsyncVideo = (src: string) => {
5353

5454
// @ts-expect-error
5555
setState({ status: "loaded", error: new Error("error") });
56+
57+
if (state.status === "error") {
58+
console.error(state.error);
59+
}
5660
};

0 commit comments

Comments
 (0)