Skip to content

Instantly share code, notes, and snippets.

@amite
Last active September 29, 2017 03:42
Show Gist options
  • Save amite/fe0f3ae218beae373aefe8c28deb4170 to your computer and use it in GitHub Desktop.
Save amite/fe0f3ae218beae373aefe8c28deb4170 to your computer and use it in GitHub Desktop.
Curry Examples
const handleChange = (fieldName) => (event) => {
this.setState(fieldName, event.target.value)
}
<input type="text" onChange={handleChange('email')} ... />
const state = {
data: {
balance: 2000,
transactions: []
},
ui: {
loading: false
}
}
const newTransaction = {
id: 1,
amount: 3000,
note: 'Lunch',
type: 'DEPOSIT' ,
date: Date.now()
}
const addTransaction = (newTransaction) => (prevState) => {
const nextState = {
...prevState,
data: {
transactions: [...prevState.data.transactions, newTransaction]
}
}
return nextState
}
const createNewTransaction = addTransaction(newTransaction)
console.log(createNewTransaction(state))
console.log(state)
const state = {
data: {
balance: 2000
},
ui: {
loading: false
}
}
const amountToAdd = 3000
const calculateNewBalance = (amountToAdd) => (state) => {
const nextState = {
...state,
data: {
balance: state.data.balance + amountToAdd
}
}
return nextState
}
const newBalance = calculateNewBalance(amountToAdd)
console.log(newBalance(state))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment