Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created December 5, 2021 23:56
Show Gist options
  • Save natafaye/8d87f2dc2b2fb207beaaa787e0dec025 to your computer and use it in GitHub Desktop.
Save natafaye/8d87f2dc2b2fb207beaaa787e0dec025 to your computer and use it in GitHub Desktop.
addLike() {
//this.state.transactionList = fdjskflds // DO NOT CHANGE STATE DIRECTLY
// let something = this.state.transactionList.push({ fdjskfldsfjklds }) // DO NOT CHANGE STATE DIRECTLY
// this.setState({ transactionList: something })
this.setState(state => ({ numLikes: state.numLikes + 1 }) )
// adding
this.setState( state => ({ transactionList: [ ...state.transactionList, "fdsfdsfsd"] }) )
this.setState( state => ({ transactionList: state.transactionList.concat(["fdsfdsfsd"]) }) )
// deleting
this.setState( state => ({ transactionList: state.transactionList.filter(t => t.id !== idToDelete ) }) )
// update one transaction
this.setState( state => ({ transactionList: state.transactionList.map(t => {
if(t.id === idToUpdate) {
return { ...t, amount: 20 }
}
else {
return t;
}
})
})
)
// simplified version of previous
this.setState( state => ({ transactionList: state.transactionList.map( t => (t.id === idToUpdate) ? { ...t, amount: 20 } : t ) }) )
// stretched out version of previous
this.setState( state => {
const newTransactionList = [...state.transactionList]
newTransactionList[3] = { ...newTransactionList[3], amount: 20 }
return { transactionList: newTransactionList }
})
//this.state.transactionList[3] = { ...transactionToUpdate, amount: 20 } // DO NOT CHANGE STATE DIRECTLY
}
// TODO
// { people: [ Simone, Maria, Chance ]}
// { people: [ Simone ]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment