This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const App = () => { | |
const { onSubmit, friends, undo } = useApp() | |
const [name, setName] = useState('') | |
const [gender, setGender] = useState('Male') | |
const onNameChange = (e) => setName(e.target.value) | |
const onGenderChange = (e) => setGender(e.target.value) | |
const resetValues = () => { | |
setName('') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useApp = () => { | |
const [state, dispatch] = useReducer(reducer, initialState) | |
const onSubmit = (friend, resetValues) => (e) => { | |
e.preventDefault() | |
if (!friend.name) return | |
dispatch({ type: 'add-friend', friend }) | |
resetValues() | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducer = (state, action) => { | |
switch (action.type) { | |
case 'set-theme': | |
return { ...state, theme: action.theme, history: insertToHistory(state) } | |
case 'add-friend': | |
return { | |
...state, | |
friends: [...state.friends, action.friend], | |
history: insertToHistory(state), | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const insertToHistory = (state) => { | |
if (state && Array.isArray(state.history)) { | |
// Do not mutate | |
const newHistory = [...state.history] | |
newHistory.push(state) | |
return newHistory | |
} | |
console.warn( | |
'WARNING! The state was attempting capture but something went wrong. Please check if the state is controlled correctly.', | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react' | |
import cx from 'classnames' | |
import female from './images/female.jpg' | |
import male from './images/male.jpg' | |
import useApp from './useApp' | |
const FriendBox = ({ gender, ...props }) => ( | |
<div | |
className={cx('box', { | |
'teal-border': gender === 'Male', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.theme-light, | |
.theme-dark { | |
box-sizing: border-box; | |
transition: all 0.15s ease-out; | |
padding: 12px; | |
min-height: 100vh; | |
} | |
.theme-light { | |
color: #145269; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducer = (state, action) => { | |
switch (action.type) { | |
case 'set-theme': | |
return { ...state, theme: action.theme, history: insertToHistory(state) } | |
case 'add-friend': | |
return { | |
...state, | |
friends: [...state.friends, action.friend], | |
history: insertToHistory(state), | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<h3>Made a mistake?</h3> | |
<div className="undo-actions"> | |
<div> | |
<button type="button" onClick={undo}> | |
Undo | |
</button> | |
</div> | |
<div> | |
<button type="button" onClick={reset}> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react' | |
import cx from 'classnames' | |
import useApp from './useApp' | |
import ThemeControl from './ThemeControl' | |
import AddFriend from './AddFriend' | |
import UndoResetControl from './UndoResetControl' | |
import Friends from './Friends' | |
import './styles.css' | |
const App = () => { | |
const { friends, theme, onSubmit, onThemeChange, undo, reset } = useApp() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const onThemeChange = (e) => { | |
dispatch({ type: 'set-theme', theme: e.target.value }) | |
} | |
return { | |
...state, | |
onSubmit, | |
undo, | |
onThemeChange, | |
} |