I have been reading a bunch of React code and I see stuff like this that I don’t understand:
handleChange = field => e => {
e.preventDefault();
/// Do something here
}
For example, if you have a curried function First, look at this function with two parameters: … const add = (x,y) => x + y add(2, 3) //=> 5 .\n\nHere it is again … const add = x => y (i.e., in curried form).\n\nHere is the same code with no arrow functions … const add = function (x) return function(y) ‘return x + y ‘ ‘stuff’ in .\n\nWe know that arrow functions work like this – someParam => returnValue so we pay attention to the return value.’ In other words, the bold text of our function adds an add(2) // returns (y = 2 + y) to a function.\n\nWe have to call it a bit differently to use our curried function … add(2)(3) // returns 5 .\n\nThis is because the first (outer) function call returns a second (inner), and only after we call the second function do we actually get the result. If we separate the calls on two lines … const add2 = add(2) // returns function(y) return 2 + y – add 2 (3) // return 5 .\n\napplying our new knowledge to your code.\n\nrelated: ”What is the difference between binding, partial application and currying?”\n\nNow that we know how that works, let\’s look at your code handle handle handles handleChange = field => e.preventDefault() /// Do something here ?\n\nWe\’ll write it without arrow functions … handleChange = function(field) return function (e.preventDefault()) // Do something here // return . ; : ?\n\nHowever, because arrow functions lexically bind this, it would actually look more like this … handleChange = function(field) return function (e.prevent()) // Do something here // return . \’.bind(this) (i.f.\n\nThe handleChange function is making a function for an assigned field.’ This is one of the most useful React techniques because you need to set up your own listeners on each input to update your applications state. By using the handleCange, we will remove all the duplicated code that would cause change listener for each field; Cool! 1 Here I did not have to lexically bind this because the original add function does not use any context, so it is not necessary to preserve it in this case.\n\nIf required – const three = a => b -> c – i.e., more than one can sequence erectiles More .\n\nA const four = a => b ++ c -> d – i.e., p + . g + +d.\n\nThree (1) (2) (3) // 6 .\n\nFour (1) (2) (3) (4) // 10 .\n\nIt can be obvious that curried functions are . below we see $ as a two-parameter function, but at the call site it seems like we can give any arguments. Currying is the abstraction of arity – ).\n\nA const $ = x => $ (k (x) ) Const add =x + y ++ Y = 1 (add (2)) // // + 2 = 3 (mult (6)) * 6 = 18 (console.7) [2 = 4 = 2 (2 (2 (2) =128), // 256 (const.\n\n\n\nA similar concept is partial application Partial app, which allows us to partially apply functions, such as currying, except that the function does not have to be defined in curried form – const partial = (f, …a) => (…b) ‘f’ – .\n\nconst add3 = (x, y and z) => x + Y + Z .\n\npartial (add3) (1, 2, 3) // 6 .\n\npartial (add3,1) (2, 3) // 6 .\n\npartial (add3, 1, 2) (3) // 6 .\n\npartial (add3,1,2,3) () // 6 .\n\npartial (add3, 1,1,1,1) (1,1,1 1, 1,1) // 3 .\n\nHere\’s a working demo of partial you can play with in your browser – .\n\nA const partial = (f, …a) => (…b) ‘…… f (.a. . (…).’ ‘It’s a sign of cii codes’ size ‘keydown’, partial (eventorname=foo), ’inputput name== event – event’.