diff --git a/hierarchical-state/README.md b/hierarchical-state/README.md index 71f6b16..f355d47 100644 --- a/hierarchical-state/README.md +++ b/hierarchical-state/README.md @@ -46,6 +46,10 @@ General clean-ups, todos and things I wish to implement for this project: something. * DONE. Turn the "lucky numbers" component into a "dice roll" component. This component will fetch from a mock HTTP/JSON API using the mocked fetch, and it will show the roll value and have a re-roll button. + * DONE (pretty decent; this shows the double fetching problem) Show the fetch count. I tried abstracting a GameApiClientClient and other things but the demo was getting + out of scope. I think I want like a global fetchCount state or something. Should I set it via props or use the Context API? + * Fix the double fetching problem. I think I need to push state up out of the GameDieRoll component. Not really sure. + I'm hoping to find multiple ways to solve this problem. ## Reference diff --git a/hierarchical-state/package.json b/hierarchical-state/package.json index 5ca0a5d..990c252 100644 --- a/hierarchical-state/package.json +++ b/hierarchical-state/package.json @@ -11,6 +11,6 @@ "esbuild": "~0.19.11" }, "scripts": { - "start": "esbuild src/app.jsx --servedir=www --outdir=www/js --bundle --serve=[::1]:8000" + "start": "esbuild src/index.jsx --servedir=www --outdir=www/js --bundle --serve=[::1]:8000" } } diff --git a/hierarchical-state/src/GameDieRoll.jsx b/hierarchical-state/src/GameDieRoll.jsx index ac2535c..56717c2 100644 --- a/hierarchical-state/src/GameDieRoll.jsx +++ b/hierarchical-state/src/GameDieRoll.jsx @@ -57,24 +57,26 @@ const loadingDieAsciiArt = ` /** * A game die ("die" as in dice 🎲). It can be re-rolled by clicking a button. */ -export function GameDieRoll() { - console.log("[DiceRoll] Render function invoked."); - const [diceRoll, setDiceRoll] = useState(null); +export function GameDieRoll(props) { + console.log("[GameDieRoll] Render function invoked."); + const [gameDieRoll, setGameDieRoll] = useState(null); const [isLoading, setIsLoading] = useState(true); + // This isn't right, I can't use this as a click handler because it basically makes a fetch outside of a useEffect + // callback, right? Again, I'm not sure how to do basic React programming. const rollDice = () => { + props.incrementFetchCount(); setIsLoading(true); - console.log("[DiceRoll] `useEffect` callback invoked. Making a 'fetch' request."); + console.log("[GameDieRoll] `useEffect` callback invoked. Making a 'fetch' request."); fetch('/dice-roll') .then(response => { - console.log("[DiceRoll] `fetch` received a response."); + console.log("[GameDieRoll] `fetch` received a response."); return response.json(); }) .then(data => { - setDiceRoll(data); + setGameDieRoll(data); setIsLoading(false); - }) - .catch(error => console.error('Unexpected error during fetch', error)); + }); }; useEffect(rollDice, []); // Empty dependency array ensures this runs once on mount and not on every render @@ -90,7 +92,7 @@ export function GameDieRoll() { return (
{gameDieNumberToAsciiArt[diceRoll]}+
{gameDieNumberToAsciiArt[gameDieRoll]}
fetchfunction has been called {fetchCount} times