Skip to content

#10_2: State Reducer (with types) #14

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: 10_1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions showcase/src/patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ const clapReducer = (state, { type, payload }) => {
const { count, countTotal } = state

switch (type) {
case 'clap':
case useClapState.types.clap:
return {
count: count + 1,
countTotal: countTotal + 1,
isClicked: true
}
case 'reset':
case useClapState.types.reset:
return payload
default:
return state
Expand Down Expand Up @@ -205,6 +205,12 @@ const useClapState = ({
}
}

useClapState.reducer = clapReducer
useClapState.types = {
clap: 'clap',
reset: 'reset'
}

/** ====================================
* 🔰Hook
useEffectAfterMount
Expand Down Expand Up @@ -324,26 +330,11 @@ const Usage = () => {
const [timesClapped, setTimeClapped] = useState(0)
const clappedTooMuch = timesClapped >= 7

const reducer = (state, { type, payload }) => {
const { count, countTotal } = state
switch (type) {
case 'clap':
return !clappedTooMuch
? {
count: count + 1,
countTotal: countTotal + 1,
isClicked: true
}
: {
count,
countTotal,
isClicked: true
}
case 'reset':
return payload
default:
return state
const reducer = (state, action) => {
if (action.type === useClapState.types.clap && clappedTooMuch) {
return state
}
return useClapState.reducer(state, action)
}

const {
Expand Down