diff --git a/README.md b/README.md index 9651a8d..41461c3 100644 --- a/README.md +++ b/README.md @@ -32,3 +32,6 @@ Fetching data from API flow 5. Call the async function inside useEffect 6. Check my console.log and put the data in the state 7. Render something on the screen based on state + +## Lifting state diagram +![Lifting state diagram](diagram.png) \ No newline at end of file diff --git a/diagram.png b/diagram.png new file mode 100644 index 0000000..d2865e8 Binary files /dev/null and b/diagram.png differ diff --git a/src/components/character-list.js b/src/components/character-list.js index 23d9f2e..1dce396 100644 --- a/src/components/character-list.js +++ b/src/components/character-list.js @@ -8,9 +8,19 @@ const CharacterList = () => { const [characters, setCharacters] = useState(null); const getCharacters = async () => { + // Get the characters data from the API const response = await axios.get('https://my-json-server.typicode.com/TechmongersNL/fs03-react/characters'); console.log(response.data); - setCharacters(response.data); + + // We want to keep track of the number of "likes" for each character in our state now too + // Add our own "likes" key into each character object + const charactersWithLikes = response.data.map((character) => { + return {...character, likes: 0} + }) + console.log(charactersWithLikes, 'character with likes') + + // Each character, now with data from the API and our own "likes" data, is saved into the local React state + setCharacters(charactersWithLikes); } // If you don't put getCharacters in a useEffect hook, getCharacters will be called (and will make an Axios request) every time CharactersList gets re-rendered @@ -22,26 +32,64 @@ const CharacterList = () => { getCharacters(); }, []); + const increaseLikes = (id) => { + // when this function is called, I want to increase the amount of likes on that character + // in order to update a character, I need to use setCharacters + + // to update the character: first we need to find the character that we want to update + // then make a copy of that character, and increase the amount of likes in it + // add that updatedCharacter back into our updatedArray + // setCharacter(updatedArray) + const updatedArray = characters.map((character) => { + if (character.id === id) { + return {...character, likes: character.likes + 1}; + } else { + return character; + } + }) + // console.log(characters, 'characters before update') + // console.log(updatedArray, 'characters after update'); + + setCharacters(updatedArray); + // console.log('increase likes was clicked with id: ' + id) + } + const getCharactersComponents = () => { - return characters.map((character, index) => { + return characters.map((character) => { return ( ) }) } + const calculateTotalLikes = () => { + let likesSum = 0; + characters.forEach((character) => { + likesSum = likesSum + character.likes; + }) + return likesSum; + } + return ( // if characters data is not null (the initial value of the characters state variable) // then I want to show Characters components // else I want to show "loading..." - characters ? getCharactersComponents() : 'Loading...' + <> +

Total number of likes: {characters ? calculateTotalLikes() : 'Loading...'}

+
+ {characters ? getCharactersComponents() : 'Loading...'} +
+ ) } diff --git a/src/components/character.js b/src/components/character.js index 51ea4eb..d6069cf 100644 --- a/src/components/character.js +++ b/src/components/character.js @@ -22,7 +22,8 @@ const Character = (props) => {

Quote

{props.quote}

- + {/* Passing props from CharacterList even further down into LikeCounter */} +
) diff --git a/src/components/like-counter.js b/src/components/like-counter.js index 762cfdb..999ca38 100644 --- a/src/components/like-counter.js +++ b/src/components/like-counter.js @@ -1,25 +1,33 @@ import {useState} from 'react'; import '../styles/like-counter.css'; -const LikeCounter = () => { - const [count, setCount] = useState(0); +const LikeCounter = (props) => { + // No more local state, it's now being passed in through props + //const [count, setCount] = useState(0); + + const count = props.likes; + const increaseLikes = props.increaseLikes; const [favorite, setFavorite] = useState(false); const whenIncreaseButtonClicked = () => { - setCount(count + 1); + increaseLikes(props.id); }; - const whenDecreaseButtonClicked = () => { - if (count > 0) { - setCount(count - 1); - } - } + + // Can you think of ways to make the decrease button work? + // Option 1: Create a new decreaseLikes function in CharacterList, and pass it down through props + // Option 2: Pass a boolean variable (also called a "flag") to increaseLikes. When the value is true, increase likes by 1. When it's false, decrease likes by 1 + // const whenDecreaseButtonClicked = () => { + // if (count > 0) { + // setCount(count - 1); + // } + // } return ( <>

Number of likes: {count}

- + {/* */}

{favorite && '⭐️'}