Photo by Glenn Carstens-Peters on Unsplash
Day 13 of 100days of code
Hello and welcome to day 13 of my challengeππ
Deleting items from the list .
First step:
First, I will create a function in the App
component called handleDeleteItems
to handle the deletion of items. Thereafter, I will pass this function as a prop to the PackingList
component as onDelete
, with the function designed to accept an "id" as a parameter.ππ
To explain better:
const handleDeleteItems = (id) => {
: This line declares a function namedhandleDeleteItems
that takes anid
as a parameter.setItems((items) => ...);
: This line uses thesetItems
function, likely obtained from theuseState
hook, to update the state of theitems
in the component.items.filter((item) =>
item.id
!== id)
: Thefilter
method is used to create a new array containing only the items that do not have the specifiedid
. Theitem.id
!== id
condition ensures that only items with differentid
values are included in the new array.The entire
items.filter(...)
expression ensures that items with the specifiedid
are removed from the array, and the new array is then set as the updated state usingsetItems
.
In summary, when handleDeleteItems
is called with an id
, it updates the items
state by creating a new array that excludes the item with the specified id
. This effectively removes the item from the list.
Second step:
In the PackingList
component, I will destructure the props. Since the Item component requires these props, I will pass them down to the Item
component and destructure them there as well. I will use the onClick event handler on the "β" button, calling the onDelete
function with the item id as a parameter. ππ
The item.id
is passed as an argument to the onDelete
function to identify which item should be deleted or removed. The "β" symbol is just the visual representation inside the button, commonly used to indicate a delete action in user interfaces.ππππ
Updating item as packed or unpacked.
First Step:
I updated the newItem
array within the FormComponent
. It was a minor modification, just an addition around line 18.ππ
second step:
Create a function named handleToggleItemππ
To explain better:
const handleToggleItem = (id) => {
: This line declares a function namedhandleToggleItem
that takes anid
as a parameter.setItems((items) => ...);
: This line uses thesetItems
function, which is likely a state updater function obtained from theuseState
hook. It's responsible for updating the state of theitems
in the component.items.map
((item) => ...);
: Themap
function is used to create a new array by iterating over each item in the currentitems
array.item.id
=== id ? { ...item, packed: !item.packed } : item
: This line checks if theid
of the current item matches the providedid
. If it does, it creates a new object (using the spread operator...
) with the same properties as the current item (...item
), but it toggles the value of thepacked
property by using!item.packed
. If theid
doesn't match, it returns the current item unchanged.The entire
items.map
(...)
expression ensures that the modification is applied to the specific item with the matchingid
, and the new array is returned.
Third step: Destructure the handleToggleItem
function received as the onToggle
prop in the PackingList
component. Additionally, pass it as props to its child component, which is the Item
component in this case.
Fourth step: I will create a checkbox before the span for each item. The text's appearance is styled based on whether the item is marked as "packed" or not, and the checkbox triggers the onToggle
function when its state changes.
To explain better:
value={item.packed}
: Thevalue
attribute is set to thepacked
property of theitem
. This likely determines whether the checkbox is checked or unchecked based on thepacked
property of the item.onChange={() => onToggle(
item.id
)}
: This is theonChange
event handler for the checkbox. It calls theonToggle
function when the checkbox state changes, passingitem.id
as an argument. TheonToggle
function is responsible for handling the toggling of the "packed" property for the corresponding item.<span style={item.packed ? { textDecoration: "line-through" } : {}}>
:This is a span element that wraps around the text content.
style={item.packed ? { textDecoration: "line-through" } : {}}
: The style attribute is conditionally applied based on the value ofitem.packed
. Ifitem.packed
istrue
, the text decoration is set to "line-through" (which typically strikes through the text). Ifitem.packed
isfalse
or undefined, an empty style object is applied, meaning no text decoration.
Derived state.
In React, derived state involves calculating or deducing state values from the current state or props of a component, rather than assigning them directly within the component. Instead of storing these derived values in the component's state, they are dynamically generated based on the existing state or props when necessary.
Utilizing derived state is advantageous when there is a need to compute or transform data based on other elements of state or props, eliminating the need to store these intermediary values explicitly in the component's state. This approach promotes cleaner and more maintainable code and prevents unnecessary re-rendering.
Tomorrow, I'll demonstrate a practical example of this while updating the stats component.