Define a pure function
A function with no side effects
It does not modify its inputs
It's repeatable
(same inputs, same outputs)
function doubleVals(arr) {
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i] * 2;
}
return arr;
}
function doubleVals(arr) {
return arr.map(v => v * 2);
}
var person = {id: 53, name: "Tim"};
function addJob(job){
person.job = job;
}
addJob("Instructor");
var person = {id: 53, name: "Tim"};
function addJob(personObj, job){
return Object.assign({},
perosnObj,
{job});
}
addJob(person, "Instructor");
var person = {id: 53, name: "Tim"};
function addJob(personObj, job){
return {...personObj, job};
}
addJob(person, "Instructor");
All changes to this.state
should be pure