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 (
{props.quote}