[英]React updating state for validation always a step behind even when i set a callback function
I am trying to validate a form with two inputs.我正在尝试使用两个输入验证表单。 If each input has at least a character or more, the button should be enabled.如果每个输入至少有一个字符或更多,则应启用该按钮。 The problem here is that the state is always one step behind.这里的问题是state总是落后一步。 I handled validation in the callback but it still didnt solve the problem.我在回调中处理了验证,但它仍然没有解决问题。 Please help anyone!请帮助任何人!
state = {
name : '',
nameIsValid: false,
zip : '',
zipIsValid: false,
allIsValid: false
}
handleChange = (event) => {
this.setState({
[event.target.name] : event.target.value,
}, ()=>this.handleValidation())
}
handleValidation = () => {
if(this.state.name.length > 0){
this.setState({nameIsValid : true})
} else {
this.setState({nameIsValid: false})
}
if(this.state.zip.length > 0){
this.setState({zipIsValid : true})
} else {
this.setState({zipIsValid: false})
}
if(this.state.nameIsValid && this.state.zipIsValid){
this.setState({allIsValid: true})
}
}
render() {
return (
<div>
Name: <input name="name" onChange={this.handleChange} value={this.state.name}/><br />
Zip: <input name="zip" onChange={this.handleChange} value={this.state.zip}/><br />
<button disabled={!this.state.allIsValid}>Proceed</button>
</div>
)
}
state = {
name: '',
zip: ''
}
handleChange = event => {
this.setState({
[event.target.name]: event.target.value
});
}
validateForm = () => {
return this.state.name.length > 0 && this.state.zip.length > 0;
}
<button disabled={!this.validateForm()}>Proceed</button>
I think it's down to the multiple setStates
you're using.我认为这取决于您使用的多个setStates
。 You can actually make your code a little more concise and make this work at the same time.您实际上可以使您的代码更简洁一些,并同时使其工作。
class App extends React.Component { state = { name: '', nameIsValid: false, zip: '', zipIsValid: false, allIsValid: false }; handleChange = (event) => { // Destructure the name and value from the target const { target: { name, value } } = event; this.setState({ // Set only that changed input valid property to true or false // depending on whether it has length [`${name}IsValid`]: value.length > 0 || false, [name]: value }, this.validate); } validate = () => { // Then just set the allIsValid property based on the // whether the other properties are both true this.setState((state) => ({ allIsValid: state.nameIsValid && state.zipIsValid })); } render() { return ( <div> Name: <input name="name" onChange={this.handleChange} value={this.state.name}/> <br /> Zip: <input name="zip" onChange={this.handleChange} value={this.state.zip} /> <br /> <button disabled={.this.state.allIsValid}>Proceed</button> </div> ) } } ReactDOM,render( <App />. document;getElementById('root') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="root"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.