I have a basic form in a react 16 app. Upon successful submission of the form I am switching a variable from false to true and I then want to display a different div once the boolean is switched.
Here is my variable declaration and form submit function:
var formSubmitted = false;
const formObj = {};
const requestInfo = (formObj) => {
formObj.modelNumber = product.modelNumber
submitForm(formObj)
.then(value => {
formSubmitted = true;
console.log(formSubmitted);
}, reason => {
// rejection
});
};
Here is the conditional HTML:
<div>Always showing</div>
{ !formSubmitted &&
<div>Form has not been submitted</div>
}
{ formSubmitted &&
<div>Form submitted successfully</div>
}
The form is submitting successfully and the console.log is showing the variable formSubmitted was successfully switched from false to true, however the HTML is not changing. I'm new to react 16 and I'm not sure how to update the component.
Thank you.
Although the variable of
formSubmittedhad changed, no state or side effects were triggered, so it didn't trigger the React render function hence the changes you expect are not reflecting on the DOM. You can useuseStateto manage the boolean value offormSubmittedso every new assignment will trigger 'render' to update the UI. Here is a snippet:And that should work.