Answer the question
In order to leave comments, you need to log in
How to check for duplicate add on client in React?
Hey!
I have 2 fields email and name and a submit button .
After clicking on the button, the form is submitted and the following code is triggered
validate(state, form) {
// сначала валидируем
...
return this.request(form);
}
request(form) {
// затем отправляем запрос
...
return fetch(...)
}
handleSubmit(e) {
e.preventDefault();
return this.validate(this.state.emailData, e.target);
}
componentDidUpdate
that will return the previous ones to me props
(which I don’t use because this method fires on every state change and there is one in my class onChange
that changes this state when text is entered in input
). componentDidUpdate
, but only after clicking on the submit button?
Answer the question
In order to leave comments, you need to log in
state: { hadSubmitted: false, };
componentDidUpdate(prevProps, prevState) {
const { hadSubmitted } = this.state;
if (hadSubmitted) {
//...validate
}
}
handleSubmit(e) {
...
this.setState({
hadSubmitted: true,
});
}
validate(state, form) {
...
this.setState({ hadSubmitted: false, })
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question