-
-
Save rbiggs/c25a2b7f96370758a19f6d1783205776 to your computer and use it in GitHub Desktop.
Adding an action to delete an item from the list component based on the key value in the message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // actions gets two arguments: state and the message we send | |
| function actions(state, message) { | |
| switch(message) { | |
| case 'updateInputValue': | |
| // Update state with new value: | |
| state.inputValue = message.value | |
| // Don't forget to return state: | |
| return state | |
| case 'addItem': | |
| // Add new item to array: | |
| state.fruits.push({ | |
| key: state.newKey++, | |
| value: state.inputValue | |
| }) | |
| // Don't forget to return state: | |
| return state | |
| case 'deleteItem': | |
| // Use the message value, | |
| // which is the key of the item the user click on, | |
| // to filter it out from the array of fruits. | |
| state.fruits = state.fruits.filter(fruit => fruit.key != message.value) | |
| // Don't forget to return state: | |
| return state | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment