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 stripLast = arr => { | |
| // split the last item from an array and return a tupple of [rest, last] | |
| const length = arr.length; | |
| const lastItem = arr[length - 1]; | |
| const restOfArr = arr.slice(0, length - 1); | |
| return [restOfArr, lastItem]; | |
| }; | |
| const createTrimStartArray = length => arr => { | |
| const startIndex = arr.length < length ? 0 : length; |
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
| function Pets() { | |
| const [pets, dispatch] = useReducer(petsReducer, initialState); | |
| const onChange = ({ target }) => { | |
| dispatch({ type: "PET_SELECTED", payload: target.value }); | |
| }; | |
| useEffect(() => { | |
| if (pets.selectedPet) { | |
| dispatch({ type: "FETCH_PET" }); |
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
| function App() { | |
| return ( | |
| <div> | |
| <Pets /> | |
| </div> | |
| ); | |
| } |
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 petsDB = { | |
| dogs: { name: "Dogs", voice: "Woof!", avatar: "🐶" }, | |
| cats: { name: "Cats", voice: "Miauuu", avatar: "🐱" } | |
| }; | |
| export function getPet(type) { | |
| return new Promise(resolve => { | |
| // simulate a fetch call | |
| setTimeout(() => { | |
| resolve(petsDB[type]); |
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
| function Pets() { | |
| const [pets, dispatch] = useReducer(petsReducer, initialState); | |
| const onChange = ({ target }) => { | |
| dispatch({ type: "PET_SELECTED", payload: target.value }); | |
| }; | |
| useEffect(() => { | |
| if (pets.selectedPet) { | |
| dispatch({ type: "FETCH_PET" }); |
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
| function useIsMountedRef(){ | |
| const isMountedRef = useRef(null); | |
| useEffect(() => { | |
| isMountedRef.current = true; | |
| return () => isMountedRef.current = false; | |
| }); | |
| return isMountedRef; | |
| } |
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
| function Pets() { | |
| const [pets, dispatch] = useReducer(petsReducer, initialState); | |
| const onChange = ({ target }) => { | |
| dispatch({ type: "PET_SELECTED", payload: target.value }); | |
| }; | |
| useEffect(() => { | |
| let mounted = true; | |
| if (pets.selectedPet) { |
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
| function App() { | |
| const [showPets, setShowPets] = useState(true); | |
| const toggle = () => { | |
| setShowPets(state => !state); | |
| }; | |
| return ( | |
| <div> | |
| <button onClick={toggle}>{showPets ? "hide" : "show"}</button> |
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
| function Pets() { | |
| const [pets, dispatch] = useReducer(petsReducer, initialState); | |
| const isMountedRef = useRef(null); | |
| const onChange = ({ target }) => { | |
| dispatch({ type: "PET_SELECTED", payload: target.value }); | |
| }; | |
| useEffect(() => { | |
| isMountedRef.current = true; |
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 initialState = { loading: false, selectedPet: "", petData: null } | |
| function petsReducer(state, action) { | |
| switch (action.type) { | |
| case "PET_SELECTED": { | |
| return { | |
| ...state, | |
| selectedPet: action.payload | |
| }; | |
| } |