I am bit puzzled as when to to bind in constructor and when not to it .I tried reading two codes but couldn't get to a conclusion as when bind is needed ? I need an explanation on the basis of two codes .
class Calculatetop extends React.Component{
constructor(props){
super(props);
this.state = this.initialState();
this.operatorStack = [];
this.operandStack = [];
this.shouldReset = false;
}
initialState() {
return {
currentDisplay:'',
};
}
reset() {
this.setState(()=> this.initialState());
this.operatorStack = [];
this.operandStack = [];
}
handleInput(input) {
let digits = ["0","1","2","3","4","5","6","7","8","9","."];
let operators = ["+","-","*","/","="];
if (input === "C") {
this.reset();
return;
}
if (digits.includes(input)) {
console.log("input is "+input);
if(this.shouldReset === true) {
this.state.currentDisplay = '';
this.shouldReset = false;
}
this.setState({
currentDisplay : this.state.currentDisplay + input
})
}
if (operators.includes(input)) {
console.log(this.operandStack);
console.log(this.operatorStack);
if(this.operatorStack.length > 0 && this.precedence(input) <= this.precedence(this.topOperator()) || input == "=") {
console.log("inside if ");
this.operandStack.push(parseFloat(this.state.currentDisplay));
console.log("this.state.currentdisplay"+this.state.currentDisplay);
this.solveStack();
let result = this.operandStack[0];
this.setState({
currentDisplay:`${result}`
})
if(input=="="){
this.operandStack = [];
this.operatorStack = [];
}
} else {
console.log("else part executed");
this.operandStack.push(parseFloat(this.state.currentDisplay));
}
if (input !== '=') {
this.operatorStack.push(input);
this.shouldReset = true;
}
}
}
topOperator() {
return this.operatorStack[this.operatorStack.length - 1];
}
solveStack() {
console.log("solvestack is executed");
while(this.operatorStack.length > 0) {
console.log(this.operandStack);
let operator = this.operatorStack.pop();
let operandTwo = this.operandStack.pop();
let operandOne = this.operandStack.pop();
this.operandStack.push(this.performOperation(operandOne,operandTwo,operator));
}
}
precedence(operator) {
return {
'+' : 1 , '-' : 1 , '*' : 2 , '/' : 2
}[operator];
}
performOperation(first,second,operator) {
if (operator === "+") {
return first + second;
}
else if (operator === "-") {
return first - second;
}
else if (operator === "*") {
return first * second;
}
else if (operator === "/") {
return first / second;
}
}
render()
{
return(
<div>
<h1>CalculatorApp</h1>
<CalculatorDisplay
currentDisplay={this.state.currentDisplay}
></CalculatorDisplay>
<CalculatorConfig inputHandler={(input) => this.handleInput(input)}></CalculatorConfig>
</div>
);
}
}
Here is the second code and I see bind being heavily used in this class but I couldn't understand as why we need to use this in second code .
class Indecisionapp extends React.Component{
constructor(props){
super(props);
this.handledeleteoptions=this.handledeleteoptions.bind(this);
this.handlepick=this.handlepick.bind(this);
this.handleaddoption=this.handleaddoption.bind(this);
this.state ={
options:[]
};
}
handledeleteoptions(){
this.setState(()=>{
return{
options:[]
};
});
}
handlepick(){
const randomnum=Math.floor(Math.random() *this.state.options.length);
const option=this.state.options[randomnum];
alert(option);
console.log(randomnum);
}
/// it takes an argument
handleaddoption(option){
console.log(option);
if(!option){
return 'Enter valid text';
}
else if(this.state.options.indexOf(option)>-1){
return 'the option already exits ';
}
this.setState((prevState)=>{
return{
options:prevState.options.concat([option])
};
});
}
render(){
const titlevar="Indecision App";
const subtitlevar='Put your life in the hands of a computer';
//const optionsvar=['one ','two','three'];
return(
<div>
<Header title={titlevar} subtitle={subtitlevar}/>
<Action
hasoptions={this.state.options.length>0}
handlepick={this.handlepick}
/>
<Options
options={this.state.options}
handledeleteoptions={this.handledeleteoptions}
/>
<Addoptions
handleaddoption={this.handleaddoption}
/>
</div>
);
}
}