在第一步结束之前一切都很好,但是当我写下“tempPhone”号码以确认 [第一步][1]
输入区域看起来像那样(单击第一个按钮后输入字段必须为空),我没有弄清楚这个问题。为什么会这样?
[1]:https ://i.stack.imgur.com/qXYB3.png
[2]:https ://i.stack.imgur.com/wRc4W.png
[问题][2]
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor() {
super();
this.state= {
otpContent: "",
input: "",
tempPhone:"123",
tempPin: "123456",
errorMsg: ""
}
this.handleChange = this.handleChange.bind(this);
this.handlePhoneSubmit = this.handlePhoneSubmit.bind(this);
this.handlePinSubmit = this.handlePinSubmit.bind(this);
}
handleChange(e) {
this.setState({[e.target.name]: e.target.value});
}
phoneInput() {
this.setState(
{
otpContent: <div>
<input type="text" name="input" onChange={this.handleChange}/>
<button onClick={this.handlePhoneSubmit}> Dogrula!</button>
</div>
}
);
}
handlePhoneSubmit() {
if(this.state.input === this.state.tempPhone){
this.setState(
{
input: ''
}
);
this.pinInput();
}
else {
this.setState({
errorMsg: "wrong phone"
});
}
}
pinInput() {
this.setState(
{
input: '',
otpContent: (<div>
<input
type="text" name="input" onChange={this.handleChange}/>
<button onClick={this.handlePinSubmit}> Pin Dogrula!</button>
</div>)
}
);
}
handlePinSubmit() {
if(this.state.input === this.state.tempPin){
this.setSuccess();
}
else {
this.setState({
errorMsg: "wrong pin"
});
}
}
setSuccess() {
this.setState(
{
otpContent: (<div>
<h3>Success!</h3>
</div>)
}
);
}
componentDidMount() {
this.phoneInput();
}
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Hi</h1>
</header>
<div className="App-intro">
{this.state.otpContent}
{this.state.errorMsg}
</div>
</div>
);
}
}
export default App;