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
| let _previousKey; | |
| function Pets() { | |
| //... | |
| useEffect(() => { | |
| _previousKey = pets.selectedPet; | |
| if (pets.selectedPet) { | |
| dispatch({ type: "FETCH_PET" }); | |
| getPet(pets.selectedPet).then(data => { |
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 _previousKeyRef = useRef(null); | |
| useEffect(() => { | |
| _previousKeyRef.current = pets.selectedPet; | |
| if (pets.selectedPet) { | |
| dispatch({ type: "FETCH_PET" }); | |
| getPet(pets.selectedPet).then(data => { | |
| if (_previousKeyRef.current === 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
| useEffect(() => { | |
| let abort = false; | |
| if (pets.selectedPet) { | |
| dispatch({ type: "FETCH_PET" }); | |
| getPet(pets.selectedPet).then(data => { | |
| if(!abort){ | |
| dispatch({ type: "FETCH_PET_SUCCESS", payload: data }); | |
| } | |
| }); |
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 | |
| }; | |
| } |
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
| useEffect(() => { | |
| let _previousKey = pets.selectedPet; | |
| if (pets.selectedPet) { | |
| dispatch({ type: "FETCH_PET" }); | |
| getPet(pets.selectedPet).then(data => { | |
| if (_previousKey === pets.selectedPet) { | |
| dispatch({ type: "FETCH_PET_SUCCESS", payload: data }); | |
| } | |
| }); | |
| } else { |
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
| export function getPet(type) { | |
| const delay = type === "cats" ? 3500 : 500; | |
| return new Promise(resolve => { | |
| // immulate fetch call | |
| setTimeout(() => { | |
| resolve(petsDB[type]); | |
| }, delay); | |
| }); | |
| } |
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
| useEffect(() => { | |
| if (pets.selectedPet) { | |
| dispatch({ type: "FETCH_PET" }); | |
| getPet(pets.selectedPet).then(data => { | |
| dispatch({ type: "FETCH_PET_SUCCESS", payload: data }); | |
| }); | |
| } else { | |
| dispatch({ type: "RESET" }); | |
| } | |
| }, [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
| class Player { | |
| } |