Skip to content

Instantly share code, notes, and snippets.

@rbiggs
Last active October 25, 2019 03:05
Show Gist options
  • Select an option

  • Save rbiggs/c25a2b7f96370758a19f6d1783205776 to your computer and use it in GitHub Desktop.

Select an option

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
// 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