Skip to content

Commit

Permalink
reduce busy work
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Sep 5, 2024
1 parent 61ae2ac commit 6ff4228
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
3 changes: 1 addition & 2 deletions exercises/01.early-web/02.problem.pempa/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { serve } from '@hono/node-server'
// 💰 gonna need this for static files
// import { serveStatic } from '@hono/node-server/serve-static'
import { serveStatic } from '@hono/node-server/serve-static'
import closeWithGrace from 'close-with-grace'
import { Hono } from 'hono'
import * as db from './db.js'
Expand Down
19 changes: 10 additions & 9 deletions exercises/02.early-react/01.problem.spa/ui/counter.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { createElement as h, useReducer, useEffect } from 'react'

// 🐨 create the initial state with count (null), loading, and error

// 🐨 create the counterReducer for handling action types "FETCH_START", "UPDATE_START", "FETCH_SUCCESS", "UPDATE_SUCCESS", "FETCH_ERROR", "UPDATE_ERROR"
// 🦉 don't forget to handle an incorrect action type!
function countReducer(state, action) {
// 🐨 handle action types for "FETCH_START"/"UPDATE_START", "FETCH_SUCCESS"/"UPDATE_SUCCESS", and "FETCH_ERROR"/"UPDATE_ERROR"
// 🦉 don't forget to throw an error on an incorrect action type!
return state
}

export function Counter() {
// 🐨 use useReducer with the counterReducer and initialState
const state = {
count: 'TODO',
loading: false,
const [state, dispatch] = useReducer(countReducer, {
count: null,
loading: true,
error: null,
}
})

// 🐨 use useEffect to fetch the count on component mount
useEffect(() => {
Expand All @@ -30,6 +30,7 @@ export function Counter() {
'div',
null,
h('h1', null, 'Count: ' + state.count),
// 🦉 notice we've deleted the form 😭
h(
'div',
{ style: { opacity: state.loading ? 0.6 : 1 } },
Expand Down
12 changes: 5 additions & 7 deletions exercises/02.early-react/01.solution.spa/ui/counter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { createElement as h, useReducer, useEffect } from 'react'

const initialState = {
count: null,
loading: true,
error: null,
}

function counterReducer(state, action) {
switch (action.type) {
case 'FETCH_START':
Expand All @@ -27,7 +21,11 @@ function counterReducer(state, action) {
}

export function Counter() {
const [state, dispatch] = useReducer(counterReducer, initialState)
const [state, dispatch] = useReducer(counterReducer, {
count: null,
loading: true,
error: null,
})

useEffect(() => {
async function fetchCount() {
Expand Down

0 comments on commit 6ff4228

Please sign in to comment.