What is Pure ?
Pure is something, which doesn’t contain anything except itself, it is real in itself. If you check the quality of pure product taking any random sample, results will be always the same.
In Javascript pure function is something, which doesn’t change any external data/state/configuration and if you provide same input it will return the same output.
e.g.
// pure function
function sum(a,b) {
return a+b;
}
function increment(state) {
return state.value +1 // not changing the state, just using it
}
// impure function
function setState((prevState, props) => {
return { prevState.value: props.value } // changing the state
}
function sum (a,b) {
someDbCall(a); // doing external things
someRestUpdateCall(b); // doing external things
return a+b;
}
Pure is something, which doesn’t contain anything except itself, it is real in itself. If you check the quality of pure product taking any random sample, results will be always the same.
In Javascript pure function is something, which doesn’t change any external data/state/configuration and if you provide same input it will return the same output.
e.g.
// pure function
function sum(a,b) {
return a+b;
}
function increment(state) {
return state.value +1 // not changing the state, just using it
}
// impure function
function setState((prevState, props) => {
return { prevState.value: props.value } // changing the state
}
function sum (a,b) {
someDbCall(a); // doing external things
someRestUpdateCall(b); // doing external things
return a+b;
}
No comments:
Post a Comment