Challenge
Task
GIven a React component that displays a list of items. The component should have two lifecycle methods: componentDidMount and componentDidUpdate.
In the componentDidMount method, you should fetch a list of items from a server and update the component's state with the new list of items. In the componentDidUpdate method, you should check if a new item has been added to the list of items. If a new item has been added, you should fetch the updated list of items from the server and update the component's state with the new list.
Your tasks are as follows:
-
Initialize the empty array of
itemsin thestatevariable. -
Implement a
componentDidMountfunction calling thefetchItemsmethod. -
Write two conditions, one that checks whether the item is in the array using
includesand the other using thefindmethod. -
Set the state variable
itemsin thefetchDatamethod. -
Add a list item looping over the items.
Hints:
-
ComponentDidMounthas no params, just calls the methodthis.fetchItems. -
Match the
newItemin includes and for find check whetheritem.namematches or not. -
Remember to add the key to the list item as
item.idalong with the name.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Spørg mig spørgsmål om dette emne
Opsummér dette kapitel
Vis virkelige eksempler
Awesome!
Completion rate improved to 3.13
Challenge
Stryg for at vise menuen
Task
GIven a React component that displays a list of items. The component should have two lifecycle methods: componentDidMount and componentDidUpdate.
In the componentDidMount method, you should fetch a list of items from a server and update the component's state with the new list of items. In the componentDidUpdate method, you should check if a new item has been added to the list of items. If a new item has been added, you should fetch the updated list of items from the server and update the component's state with the new list.
Your tasks are as follows:
-
Initialize the empty array of
itemsin thestatevariable. -
Implement a
componentDidMountfunction calling thefetchItemsmethod. -
Write two conditions, one that checks whether the item is in the array using
includesand the other using thefindmethod. -
Set the state variable
itemsin thefetchDatamethod. -
Add a list item looping over the items.
Hints:
-
ComponentDidMounthas no params, just calls the methodthis.fetchItems. -
Match the
newItemin includes and for find check whetheritem.namematches or not. -
Remember to add the key to the list item as
item.idalong with the name.
Tak for dine kommentarer!