function concatenateAll() {
console.log(arguments); } concatenateAll("venkat","test","ch");
function concatenateAll() {
console.log(arguments); } concatenateAll("venkat","test","ch");
function volume(l, w, h) {
return l * w * h;
}
const curried = _.curry(volume);
volume(2, 3, 4); // 24
curried(2)(3)(4); // 24
volume takes three arguments, but once curried we can instead pass in each argument to three nested functions.function volume1(length) {
return function(width) {
return function(height) {
return height * width * length;
}
}
}
volume1(2)(3)(4); // 24
log(date, importance, message) that formats and outputs the information. In real projects such functions have many useful features like sending logs over the network, here we’ll just use alert:function log(date, importance, message) {
alert(`[${date.getHours()}:${date.getMinutes()}] [${importance}] ${message}`);
}
log = _.curry(log);
log works normally:log(new Date(), "DEBUG", "some debug"); // log(a, b, c)
log(new Date())("DEBUG")("some debug"); // log(a)(b)(c)
// logNow will be the partial of log with fixed first argument
let logNow = log(new Date());
// use it
logNow("INFO", "message"); // [HH:mm] INFO message
logNow is log with the fixed first argument, in other words, “partially applied function” or “partial” for short.let debugNow = logNow("DEBUG");
debugNow("message"); // [HH:mm] DEBUG message
log is still callable normally.makeStyles / withStyles / styled invocations. For instance the color red wins in this case:updateState(event) {
const {name, value} = event.target;
let user = this.state.user; // this is a reference, not a copy...
user[name] = value; // so this mutates state ?
return this.setState({user});
}
Never mutatethis.statedirectly, as callingsetState()afterwards may replace the mutation you made. Treatthis.stateas if it were immutable.
updateState(event) {
const {name, value} = event.target;
let user = Object.assign({}, this.state.user);
user[name] = value;
return this.setState({user});
}
updateState(event) {
const {name, value} = event.target;
let user = {...this.state.user, [name]: value};
this.setState({user});
}
updateState({target}) {
this.setState({user: {...this.state.user, [target.name]: target.value}});
}
// Import at the top:
import update from 'immutability-helper';
updateState({target}) {
let user = update(this.state.user, {$merge: {[target.name]: target.value}});
this.setState({user});
}
// At top, import immutable
import { Map } from 'immutable';
// Later, in constructor...
this.state = {
// Create an immutable map in state using immutable.js
user: Map({ firstName: 'Cory', lastName: 'House'})
};
updateState({target}) {
// this line returns a new user object assuming an immutable map is stored in state.
let user = this.state.user.set(target.name, target.value);
this.setState({user});
}
setState() does not immediately mutate this.state but creates a pending state transition. Accessing this.state after calling this method can potentially return the existing value.
updateState({target}) {
this.setState({user: {...this.state.user, [target.name]: target.value}});
doSomething(this.state.user) // Uh oh, setState merely schedules a state change, so this.state.user may still have old value
}
updateState({target}) {
this.setState((prevState) => {
const updatedUser = {...prevState.user, [target.name]: target.value}; // use previous value in state to build new state...
return { user: updatedUser }; // And what I return here will be set as the new state
}, () => this.doSomething(this.state.user); // Now I can safely utilize the new state I've created to call other funcs...
);
}
The function declaration defines a function with the specified parameters. The function keyword can be used to define a function ins...