Last active
September 29, 2017 03:42
-
-
Save amite/fe0f3ae218beae373aefe8c28deb4170 to your computer and use it in GitHub Desktop.
Curry Examples
This file contains 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
const handleChange = (fieldName) => (event) => { | |
this.setState(fieldName, event.target.value) | |
} | |
<input type="text" onChange={handleChange('email')} ... /> |
This file contains 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
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) |
This file contains 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
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