1

I make a very simple todo list and should be next to each generated items a delete-btn. How can I write this function via OnClick()? i have in App.js quite normal array and now should be deleted with Delete-btn any added items.

ShowListComponent:

export default function ShowList ({items}){
    const isEmpty = "Die Eingabe Feld muss nicht leer sein";


    return(
        <div className='itemList'>
            {items.map((item) => (
                <ul className='itemContainer'>

                    <li className='item-name'>
                        {item.itemName}
                    </li>

                    <div className='delete-btn'>
                        <Button type="primary" danger ghost>
                            Delete
                        </Button>
                    </div>

                </ul>
            ))}
        </div>
    );
}

List Component:

export default function List ({items, setItems}){

const [inputValue, setInputValue] = useState('');

const AddButtonClick = () => {

    if (inputValue === ""){
        alert("Eingabefeld ist Leer");
        return false
    } else {
        const newItem ={
            itemName: inputValue,
        };

        const newItems = [...items,newItem];

        setItems(newItems);
        setInputValue('');
    }

}

return(
    <>

        <Input value={inputValue} onChange={(event) =>
            setInputValue(event.target.value)}
               name='input'
               className='addItemInput'
               placeholder="Artikel hinzufügen"
        />

        <Button
            type="primary"
            className='btn'
            onClick={() => AddButtonClick()}
        >Hinzufügen</Button>

    </>
)

}

1
  • Are you asking how to remove an item from an array so React knows the array has been updated? How are the mechanics different from how you're already adding an item? Commented Mar 22, 2022 at 14:38

1 Answer 1

2

like the AddButtonClick() create deleteButtonClick:

const deleteButtonClick = (index) => {
        const newItems = items && items.filter((element , i) => i !== index);
        setItems(newItems);
}

pass it to ShowList component and use it like so:

export default function ShowList ({items, deleteButtonClick}){
    const isEmpty = "Die Eingabe Feld muss nicht leer sein";


    return(
        <div className='itemList'>
            {items.map((item, index) => (
                <ul className='itemContainer'>

                    <li className='item-name'>
                        {item.itemName}
                    </li>

                    <div className='delete-btn'>
                        <Button type="primary" danger ghost onClick={() => deleteButtonClick(index)}>
                            Delete
                        </Button>
                    </div>

                </ul>
            ))}
        </div>
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.