如何解决运行测试时,React App冻结浏览器页面
我是React的新手,目前正在使用具有React框架的基本功能的计算器应用程序。这是freecodecamp的前端库项目的一部分。我的代码可以正常工作,因为它可以进行所有计算,但是每次尝试运行测试时,网页都会冻结。这是我的代码;
class Calculator extends React.Component{
constructor(props){
super(props)
this.state = {
display: "0",clickedNumber: false,clickedOperator: false,clickedDecimal: false,}
this.handleClear = this.handleClear.bind(this)
this.inputNums = this.inputNums.bind(this)
this.doEquals = this.doEquals.bind(this)
this.inputOperator = this.inputOperator.bind(this)
this.inputDecimal = this.inputDecimal.bind(this)
}
handleClear(){
this.setState({display: "0",clickedDecimal: false})
}
inputNums(Buttons){
if(!this.state.clickedNumber && Buttons.target.innerText !== "0")
{this.setState({display: Buttons.target.innerText,clickedNumber: true,clickedDecimal: false})}
else if(this.state.clickedNumber){
this.setState({display: this.state.display + Buttons.target.innerText,})
}
}
inputOperator(operator){
if(!this.state.clickedOperator){
this.setState({
display: this.state.display + operator.target.innerText,clickedDecimal: false })
}
}
inputDecimal(decimal){
if(!this.state.clickedDecimal){
this.setState({
display: this.state.display + decimal.target.innerText,clickedDecimal: true,clickedOperator:false
})
}
}
doEquals(){
function makingRequestedOperation(arr,operIndex,oper){
let num1 = [];
let num2=[];
let info = {
}
let result;
for(var j = parseInt(operIndex)-1; j>-1; j--){
if(!isNaN(arr[j]) || arr[j] === "."){
num1.push(arr[j]);
info.firstNumIndex = j;
}else if(isNaN(arr[j]) && arr[j] !== "."){
break;
}
}
num1.reverse();
for(var i = parseInt(operIndex)+1; i<arr.length; i++){
if(!isNaN(arr[i]) || arr[i] === "."){
num2.push(arr[i]);
info.lastNumIndex = i;
}else if(isNaN(arr[i]) && arr[i] !== "."){
break;
}
}
const deleteCount = num1.concat(oper,num2);
let operator1 = num1.join("");
let operator2 = num2.join("");
if(oper==="+"){
result = parseFloat(operator1) + parseFloat(operator2);
}else if(oper ==="-"){
result = parseFloat(operator1) - parseFloat(operator2)
}else if(oper ==="*"){
result = parseFloat(operator1) * parseFloat(operator2);
}else if(oper ==="/"){
result = parseFloat(operator1) / parseFloat(operator2);
}
info.result = result;
arr.splice(info.firstNumIndex,deleteCount.length,info.result);
return arr;
}
let arr = this.state.display.split("");
while(arr.length>1){
let sumIndex = arr.indexOf("+");
let divIndex = arr.indexOf("/");
let multIndex = arr.indexOf("*");
let subIndex = arr.indexOf("-")
if(divIndex > 0 && multIndex >0){
if(divIndex < multIndex){
arr = makingRequestedOperation(arr,divIndex,"/");
}else{
arr = makingRequestedOperation(arr,multIndex,"*");
}
}else{
if(divIndex > 0){
arr = makingRequestedOperation(arr,"/");
}else if(multIndex >0){
arr = makingRequestedOperation(arr,"*");
}else if(sumIndex >0 && subIndex > 0){
if(sumIndex < subIndex){
arr = makingRequestedOperation(arr,sumIndex,"+");
}else{
arr = makingRequestedOperation(arr,subIndex,"-");
}
}else if(subIndex >0){
arr = makingRequestedOperation(arr,"-");
}else{
arr = makingRequestedOperation(arr,"+");
}
}
}
arr = Math.round(arr*100000)/100000;
this.setState({
display: arr.toString()
})
}
render(){
const buttonsData =
[{id:"zero",value: "0"},{id:"one",value: "1"},{id:"two",value: '2'},{id:"three",value: "3"},{id:"four",value: "4"},{id:"five",value: "5"},{id:"six",value: "6"},{id:"seven",value: "7"},{id:"eight",value: "8"},{id:"nine",value: "9"}];
const operators = [{
id:"add",value: "+"},{ id:"subtract",value: "-"
},{id:"multiply",value: "*"},{id:"divide",value: "/"}]
return(
<div className = "App">
<Display
display = {this.state.display}
/>
{buttonsData.map((data) =>
(<Buttons
id = {data.id}
name = {data.value}
key = {data.id}
onClick = {this.inputNums}
/>
)) }
<Buttons
id = "decimal"
onClick = {this.inputDecimal}
name = "."
/>
{operators.map((operator) =>(<Buttons
id = {operator.id}
name = {operator.value}
key = {operator.id}
onClick = {this.inputOperator}
/>))}
<Buttons
id= "equals"
name = "="
onClick = {this.doEquals}
/>
<Buttons
id = "clear"
onClick = {this.handleClear}
name = "Clear"
/>
</div>
)
}
}
我认为问题可能出在doEquals函数上,但无法解决。我该如何解决这个问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。