reactjs - this.state 反应
问题描述
我是学生,第一次做这种项目,也是第一次使用 React 和这种语法。我已经遵循了一些教程,但现在被卡住了。
当您单击时,所有数字应加在一起。
我在哪里做错了?是我解析输入的地方吗?我整个周末都尝试了不同的方法,但没有成功。请帮助我更进一步。
import React, { Component } from "react";
import './App.css';
import Button from './components/Buttons';
import Input from './components/Inputs';
import ClearButton from './components/ClearButtons';
class App extends Component {
constructor(props) {
super(props);
this.state = {
input: "",
operator: ""
};
}
addToInput = val => {
this.setState({ input: this.state.input + val });
}
// If this.state.input is not empty then add zero
// If this.state.input is not blank (another number has already been added in)
addZeroToInput = val => {
if (this.state.input !== "") {
this.setState({ input: this.state.input + val });
}
};
// Input = blank
clearInput = () => {
this.setState({ input: "" });
};
add = () => {
this.setState({ input: ""});
this.state.operator = "plus";
if (this.state.operator == "plus") {
this.setState({
input:
parseInt(this.state.input) +
parseInt(this.state.input)
});
}
};
mult = () => {
this.setState({ input: "" });
this.state.operator = "multiply";
if (this.state.operator == "multiply") {
this.setState({
input:
parseInt(this.state.input) *
parseInt(this.state.input)
});
}
};
render() {
return (
<div className="App">
<div className="wrapper">
<div className="row">
<Input>{this.state.input}</Input>
</div>
<div className="row">
<Button handleClick={this.addToInput}>7</Button>
<Button handleClick={this.addToInput}>8</Button>
<Button handleClick={this.addToInput}>9</Button>
</div>
<div className="row">
<Button handleClick={this.addToInput}>4</Button>
<Button handleClick={this.addToInput}>5</Button>
<Button handleClick={this.addToInput}>6</Button>
</div>
<div className="row">
<Button handleClick={this.addToInput}>1</Button>
<Button handleClick={this.addToInput}>2</Button>
<Button handleClick={this.addToInput}>3</Button>
</div>
<div className="row">
<Button handleClick={this.add}>+</Button>
<Button handleClick={this.addZeroToInput}>0</Button>
<Button handleClick={this.multiply}>*</Button>
</div>
<div className="row">
<ClearButton handleClear={this.clearInput}>Clear</ClearButton>
</div>
</div>
</div>
);
}
}
export default App;
解决方案
现在当我点击 4444 + 它显示 8888 但它应该显示 16。
您的添加功能包括:
this.setState({
input:
parseInt(this.state.input) +
parseInt(this.state.input)
});
在这种情况下,您this.state.input
将其"4444"
解析为整数并基本上将其添加到自身,这就是为什么它是8888
.
如果您想要的输出是将每个数字彼此相加,如 中4+4+4+4
,那么您需要this.state.input
按每个数字拆分,将其解析为整数,然后将其相加。
> "4444".split("")
[ '4', '4', '4', '4' ]
> "4444".split("").map(Number)
[ 4, 4, 4, 4 ]
> "4444".split("").map(Number).reduce((x, y) => x + y)
16
不相关,但您应该遵循@Hemadri 关于如何设置 Button 的建议handleClick
。或者,或者,只是这样做:
<button onClick={(e) => this.addToInput(e)}>1</button>
addToInput = event => {
this.setState({input: this.state.input + event.target.innerText});
}
另外不要直接改变状态,使用setState()
函数。
你也可以结合这个:
this.setState({input: ""});
this.state.operator = "plus";
进入这个:
this.setState({
input: "",
operator: "plus"
});
并摆脱,if (this.state.operator == "plus")
因为您只是将运算符设置为,plus
因此您无需检查它。
推荐阅读
- python - 返回列表中最常见元素的更有效方法
- sql - 如何获得日期 + 时间的确定性计算值
- javascript - 如何在嵌套的ngFor(Angular)中发送数组的索引
- php - 使用 user_id 存储线程并在 laravel 中使用线程和用户 id 发布
- amazon-web-services - AWS CodePipeline 因 InternalFailure 而失败
- sql - 如果计数器大于一,则将计数器附加到另一列查询中
- javascript - 这个算法的大 O 是什么?
- trino - 每个节点上的 Trino 目录
- react-native - 当它到达 SectionList 中的部分的开头时停止滚动
- reactjs - 错误:计数器按钮需要点击两次才能从 0 变为 1