React JS- Is React re-render the web page after form submitting?












-1














I am so new in React JS. I am not maintaining a code that is written by someone else. My question may be too basic, sorry for this.



There is a form in the page. Is page is re-rendered after clicking the Submit button ? In my onSubmit method, there is not a code piece that re-render the web page. For example, after submitting the form, I want to set a label "Register is successful or not". Also if there is a validation error in the form, I want to colorize the input fields.



Actually I do not know when react re-renders page and which part of the page it renders ? How can I force React to re-render somewhere in the page ?



This is my onSubmit method;



onSubmit(e) {
e.preventDefault();

this.setState({
loading: true,
});

setTimeout(() => {
this.setState({
loading: false,
});
}, 1000);

const data = this.constructFormData();

fetch('/register', {
method: 'POST',
body: data,
})
.then(p => {
if (p.status === 200) {
return p.json();
}

throw '500';
})
.catch(err => {
this.setState({
error: true,
});
});
}


And below is my form definition ;



<form
id="register-form"
method="POST"
onSubmit={this.onSubmit}
autoComplete="off"
>









share|improve this question




















  • 1




    Can I recommend you to put this.setState({ loading: false, }); in the promise .then(p => { if (p.status === 200) { return p.json(); } throw '500'; }) That will stop the loading as soon as you get the response which is much better than fixed 1 second with setTimeout.
    – Mirakurun
    yesterday


















-1














I am so new in React JS. I am not maintaining a code that is written by someone else. My question may be too basic, sorry for this.



There is a form in the page. Is page is re-rendered after clicking the Submit button ? In my onSubmit method, there is not a code piece that re-render the web page. For example, after submitting the form, I want to set a label "Register is successful or not". Also if there is a validation error in the form, I want to colorize the input fields.



Actually I do not know when react re-renders page and which part of the page it renders ? How can I force React to re-render somewhere in the page ?



This is my onSubmit method;



onSubmit(e) {
e.preventDefault();

this.setState({
loading: true,
});

setTimeout(() => {
this.setState({
loading: false,
});
}, 1000);

const data = this.constructFormData();

fetch('/register', {
method: 'POST',
body: data,
})
.then(p => {
if (p.status === 200) {
return p.json();
}

throw '500';
})
.catch(err => {
this.setState({
error: true,
});
});
}


And below is my form definition ;



<form
id="register-form"
method="POST"
onSubmit={this.onSubmit}
autoComplete="off"
>









share|improve this question




















  • 1




    Can I recommend you to put this.setState({ loading: false, }); in the promise .then(p => { if (p.status === 200) { return p.json(); } throw '500'; }) That will stop the loading as soon as you get the response which is much better than fixed 1 second with setTimeout.
    – Mirakurun
    yesterday
















-1












-1








-1







I am so new in React JS. I am not maintaining a code that is written by someone else. My question may be too basic, sorry for this.



There is a form in the page. Is page is re-rendered after clicking the Submit button ? In my onSubmit method, there is not a code piece that re-render the web page. For example, after submitting the form, I want to set a label "Register is successful or not". Also if there is a validation error in the form, I want to colorize the input fields.



Actually I do not know when react re-renders page and which part of the page it renders ? How can I force React to re-render somewhere in the page ?



This is my onSubmit method;



onSubmit(e) {
e.preventDefault();

this.setState({
loading: true,
});

setTimeout(() => {
this.setState({
loading: false,
});
}, 1000);

const data = this.constructFormData();

fetch('/register', {
method: 'POST',
body: data,
})
.then(p => {
if (p.status === 200) {
return p.json();
}

throw '500';
})
.catch(err => {
this.setState({
error: true,
});
});
}


And below is my form definition ;



<form
id="register-form"
method="POST"
onSubmit={this.onSubmit}
autoComplete="off"
>









share|improve this question















I am so new in React JS. I am not maintaining a code that is written by someone else. My question may be too basic, sorry for this.



There is a form in the page. Is page is re-rendered after clicking the Submit button ? In my onSubmit method, there is not a code piece that re-render the web page. For example, after submitting the form, I want to set a label "Register is successful or not". Also if there is a validation error in the form, I want to colorize the input fields.



Actually I do not know when react re-renders page and which part of the page it renders ? How can I force React to re-render somewhere in the page ?



This is my onSubmit method;



onSubmit(e) {
e.preventDefault();

this.setState({
loading: true,
});

setTimeout(() => {
this.setState({
loading: false,
});
}, 1000);

const data = this.constructFormData();

fetch('/register', {
method: 'POST',
body: data,
})
.then(p => {
if (p.status === 200) {
return p.json();
}

throw '500';
})
.catch(err => {
this.setState({
error: true,
});
});
}


And below is my form definition ;



<form
id="register-form"
method="POST"
onSubmit={this.onSubmit}
autoComplete="off"
>






javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Praveen Kumar Purushothaman

131k23135181




131k23135181










asked yesterday









mhendek

47110




47110








  • 1




    Can I recommend you to put this.setState({ loading: false, }); in the promise .then(p => { if (p.status === 200) { return p.json(); } throw '500'; }) That will stop the loading as soon as you get the response which is much better than fixed 1 second with setTimeout.
    – Mirakurun
    yesterday
















  • 1




    Can I recommend you to put this.setState({ loading: false, }); in the promise .then(p => { if (p.status === 200) { return p.json(); } throw '500'; }) That will stop the loading as soon as you get the response which is much better than fixed 1 second with setTimeout.
    – Mirakurun
    yesterday










1




1




Can I recommend you to put this.setState({ loading: false, }); in the promise .then(p => { if (p.status === 200) { return p.json(); } throw '500'; }) That will stop the loading as soon as you get the response which is much better than fixed 1 second with setTimeout.
– Mirakurun
yesterday






Can I recommend you to put this.setState({ loading: false, }); in the promise .then(p => { if (p.status === 200) { return p.json(); } throw '500'; }) That will stop the loading as soon as you get the response which is much better than fixed 1 second with setTimeout.
– Mirakurun
yesterday














2 Answers
2






active

oldest

votes


















3















Actually I do not know when react re-renders page and which part of the page it renders ?




It renders the page once, then it only rerenders a component where the state was changed using setState. If that rerender adds new elements, they get rendered too.




How can I force React to re-render somewhere in the page




Move the data that changes into the state and call setState if it changes.






share|improve this answer























  • do you mean the all page from the component ? For example I have a web page and my class definiton starts with class App extends Component { ... I have 4 textbox in this page. Can I render only one textbox in this page ?
    – mhendek
    yesterday










  • @mhendek then add a state to App that stores which textbox to show, then inside render() only return the textbox the state indicates
    – Jonas Wilms
    yesterday



















1














whenever you call setState(), form re-renders.
You can learn about setState in doc.
You can force re-render using forceUpdate(), but it is not advisable. Alternatively, you can do this.setState(this.state)






share|improve this answer





















  • Is React renders as soon as the state was changed ? In my case, I can set a state value (for example a label text) before submitting form.Will it be rendered immediately ?
    – mhendek
    yesterday










  • SetState is asynchronous. The function that is executing will complete it's execution first, then only it will re-render.
    – Ashish
    yesterday











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943928%2freact-js-is-react-re-render-the-web-page-after-form-submitting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3















Actually I do not know when react re-renders page and which part of the page it renders ?




It renders the page once, then it only rerenders a component where the state was changed using setState. If that rerender adds new elements, they get rendered too.




How can I force React to re-render somewhere in the page




Move the data that changes into the state and call setState if it changes.






share|improve this answer























  • do you mean the all page from the component ? For example I have a web page and my class definiton starts with class App extends Component { ... I have 4 textbox in this page. Can I render only one textbox in this page ?
    – mhendek
    yesterday










  • @mhendek then add a state to App that stores which textbox to show, then inside render() only return the textbox the state indicates
    – Jonas Wilms
    yesterday
















3















Actually I do not know when react re-renders page and which part of the page it renders ?




It renders the page once, then it only rerenders a component where the state was changed using setState. If that rerender adds new elements, they get rendered too.




How can I force React to re-render somewhere in the page




Move the data that changes into the state and call setState if it changes.






share|improve this answer























  • do you mean the all page from the component ? For example I have a web page and my class definiton starts with class App extends Component { ... I have 4 textbox in this page. Can I render only one textbox in this page ?
    – mhendek
    yesterday










  • @mhendek then add a state to App that stores which textbox to show, then inside render() only return the textbox the state indicates
    – Jonas Wilms
    yesterday














3












3








3







Actually I do not know when react re-renders page and which part of the page it renders ?




It renders the page once, then it only rerenders a component where the state was changed using setState. If that rerender adds new elements, they get rendered too.




How can I force React to re-render somewhere in the page




Move the data that changes into the state and call setState if it changes.






share|improve this answer















Actually I do not know when react re-renders page and which part of the page it renders ?




It renders the page once, then it only rerenders a component where the state was changed using setState. If that rerender adds new elements, they get rendered too.




How can I force React to re-render somewhere in the page




Move the data that changes into the state and call setState if it changes.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









Jonas Wilms

54.9k42750




54.9k42750












  • do you mean the all page from the component ? For example I have a web page and my class definiton starts with class App extends Component { ... I have 4 textbox in this page. Can I render only one textbox in this page ?
    – mhendek
    yesterday










  • @mhendek then add a state to App that stores which textbox to show, then inside render() only return the textbox the state indicates
    – Jonas Wilms
    yesterday


















  • do you mean the all page from the component ? For example I have a web page and my class definiton starts with class App extends Component { ... I have 4 textbox in this page. Can I render only one textbox in this page ?
    – mhendek
    yesterday










  • @mhendek then add a state to App that stores which textbox to show, then inside render() only return the textbox the state indicates
    – Jonas Wilms
    yesterday
















do you mean the all page from the component ? For example I have a web page and my class definiton starts with class App extends Component { ... I have 4 textbox in this page. Can I render only one textbox in this page ?
– mhendek
yesterday




do you mean the all page from the component ? For example I have a web page and my class definiton starts with class App extends Component { ... I have 4 textbox in this page. Can I render only one textbox in this page ?
– mhendek
yesterday












@mhendek then add a state to App that stores which textbox to show, then inside render() only return the textbox the state indicates
– Jonas Wilms
yesterday




@mhendek then add a state to App that stores which textbox to show, then inside render() only return the textbox the state indicates
– Jonas Wilms
yesterday













1














whenever you call setState(), form re-renders.
You can learn about setState in doc.
You can force re-render using forceUpdate(), but it is not advisable. Alternatively, you can do this.setState(this.state)






share|improve this answer





















  • Is React renders as soon as the state was changed ? In my case, I can set a state value (for example a label text) before submitting form.Will it be rendered immediately ?
    – mhendek
    yesterday










  • SetState is asynchronous. The function that is executing will complete it's execution first, then only it will re-render.
    – Ashish
    yesterday
















1














whenever you call setState(), form re-renders.
You can learn about setState in doc.
You can force re-render using forceUpdate(), but it is not advisable. Alternatively, you can do this.setState(this.state)






share|improve this answer





















  • Is React renders as soon as the state was changed ? In my case, I can set a state value (for example a label text) before submitting form.Will it be rendered immediately ?
    – mhendek
    yesterday










  • SetState is asynchronous. The function that is executing will complete it's execution first, then only it will re-render.
    – Ashish
    yesterday














1












1








1






whenever you call setState(), form re-renders.
You can learn about setState in doc.
You can force re-render using forceUpdate(), but it is not advisable. Alternatively, you can do this.setState(this.state)






share|improve this answer












whenever you call setState(), form re-renders.
You can learn about setState in doc.
You can force re-render using forceUpdate(), but it is not advisable. Alternatively, you can do this.setState(this.state)







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Ashish

534519




534519












  • Is React renders as soon as the state was changed ? In my case, I can set a state value (for example a label text) before submitting form.Will it be rendered immediately ?
    – mhendek
    yesterday










  • SetState is asynchronous. The function that is executing will complete it's execution first, then only it will re-render.
    – Ashish
    yesterday


















  • Is React renders as soon as the state was changed ? In my case, I can set a state value (for example a label text) before submitting form.Will it be rendered immediately ?
    – mhendek
    yesterday










  • SetState is asynchronous. The function that is executing will complete it's execution first, then only it will re-render.
    – Ashish
    yesterday
















Is React renders as soon as the state was changed ? In my case, I can set a state value (for example a label text) before submitting form.Will it be rendered immediately ?
– mhendek
yesterday




Is React renders as soon as the state was changed ? In my case, I can set a state value (for example a label text) before submitting form.Will it be rendered immediately ?
– mhendek
yesterday












SetState is asynchronous. The function that is executing will complete it's execution first, then only it will re-render.
– Ashish
yesterday




SetState is asynchronous. The function that is executing will complete it's execution first, then only it will re-render.
– Ashish
yesterday


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943928%2freact-js-is-react-re-render-the-web-page-after-form-submitting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas