{"id":73447,"date":"2020-01-15T18:00:28","date_gmt":"2020-01-15T12:30:28","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=73447"},"modified":"2024-04-12T14:23:13","modified_gmt":"2024-04-12T08:53:13","slug":"setstate","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/","title":{"rendered":"Sets"},"content":{"rendered":"<h4><strong>Sets<\/strong><\/h4>\n<p>setState() schedules an update to a component\u2019s state object. When state changes, the component responds by re-rendering. In React, both this.props and this.state represent the rendered values, i.e. what\u2019s currently on the screen.<\/p>\n<p>Calls to setState are asynchronous &#8211; don\u2019t rely on this.state to reflect the new value immediately after calling setState. Pass an updater function instead of an object if you need to compute values based on the current state. Example of code that will not behave as expected:<\/p>\n<p>incrementCount() {<\/p>\n<p>\/\/ Note: this will *not* work as intended.<\/p>\n<p>this.setState({count: this.state.count + 1});<\/p>\n<p>}<\/p>\n<p>handleSomething() {<\/p>\n<p>\/\/ Let&#8217;s say `this.state.count` starts at 0.<\/p>\n<p>this.incrementCount();<\/p>\n<p>this.incrementCount();<\/p>\n<p>this.incrementCount();<\/p>\n<p>\/\/ When React re-renders the component, `this.state.count` will be 1, but you expected 3.<\/p>\n<p>\/\/ This is because `incrementCount()` function above reads from `this.state.count`,<\/p>\n<p>\/\/ but React doesn&#8217;t update `this.state.count` until the component is re-rendered.<\/p>\n<p>\/\/ So `incrementCount()` ends up reading `this.state.count` as 0 every time, and sets it to 1.<\/p>\n<p>}<\/p>\n<p>Currently, setState is asynchronous inside event handlers. This ensures, for example, that if both Parent and Child call setState during a click event, Child isn\u2019t re-rendered twice. Instead, React \u201cflushes\u201d the state updates at the end of the browser event. This results in significant performance improvements in larger apps. This is an implementation detail so avoid relying on it directly. In the future versions, React will batch updates by default in more cases.<\/p>\n<p>How do I update state with values that depend on the current state?<\/p>\n<p>Pass a function instead of an object to setState to ensure the call always uses the most updated version of state.<\/p>\n<p>What is the difference between passing an object or a function in setState?<\/p>\n<p>Passing an update function allows you to access the current state value inside the updater. Since setState calls are batched, this lets you chain updates and ensure they build on top of each other instead of conflicting:<\/p>\n<p>incrementCount() {<\/p>\n<p>this.setState((state) =&gt; {<\/p>\n<p>\/\/ Important: read `state` instead of `this.state` when updating.<\/p>\n<p>return {count: state.count + 1}<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>handleSomething() {<\/p>\n<p>\/\/ Let&#8217;s say `this.state.count` starts at 0.<\/p>\n<p>this.incrementCount();<\/p>\n<p>this.incrementCount();<\/p>\n<p>this.incrementCount();<\/p>\n<p>\/\/ If you read `this.state.count` now, it would still be 0.<\/p>\n<p>\/\/ But when React re-renders the component, it will be 3.<\/p>\n<p>}<\/p>\n<p><strong>Synchronous this.state<\/strong><\/p>\n<p>React intentionally \u201cwaits\u201d until all components call setState() in their event handlers before starting to re-render. This boosts performance by avoiding unnecessary re-renders. However, you might still be wondering why React doesn\u2019t just update this.state immediately without re-rendering.<\/p>\n<p>There are two main reasons:<\/p>\n<ul>\n<li>This would break the consistency between props and state, causing issues that are very hard to debug.<\/li>\n<li>This would make some of the new features we\u2019re working on impossible to implement.<\/li>\n<\/ul>\n<h3><strong>Apply for Core Java Developer Certification Now!!<\/strong><\/h3>\n<p><a href=\"https:\/\/www.vskills.in\/certification\/certified-core-java-developer\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.vskills.in\/certification\/certified-core-java-developer<\/a><\/p>\n<h4><strong><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-core-java-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>Sets setState() schedules an update to a component\u2019s state object. When state changes, the component responds by re-rendering. In React, both this.props and this.state represent the rendered values, i.e. what\u2019s currently on the screen. Calls to setState are asynchronous &#8211; don\u2019t rely on this.state to reflect the new value immediately after calling setState. Pass an&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[229,8528],"tags":[8565],"class_list":["post-73447","page","type-page","status-publish","hentry","category-core-java","category-reactjs","tag-setstate"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Sets - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sets - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Sets setState() schedules an update to a component\u2019s state object. When state changes, the component responds by re-rendering. In React, both this.props and this.state represent the rendered values, i.e. what\u2019s currently on the screen. Calls to setState are asynchronous &#8211; don\u2019t rely on this.state to reflect the new value immediately after calling setState. Pass an...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:53:13+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/\",\"name\":\"Sets - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-15T12:30:28+00:00\",\"dateModified\":\"2024-04-12T08:53:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sets\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sets - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/","og_locale":"en_US","og_type":"article","og_title":"Sets - Tutorial","og_description":"Sets setState() schedules an update to a component\u2019s state object. When state changes, the component responds by re-rendering. In React, both this.props and this.state represent the rendered values, i.e. what\u2019s currently on the screen. Calls to setState are asynchronous &#8211; don\u2019t rely on this.state to reflect the new value immediately after calling setState. Pass an...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:53:13+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/","name":"Sets - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-15T12:30:28+00:00","dateModified":"2024-04-12T08:53:13+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/setstate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Sets"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/73447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=73447"}],"version-history":[{"count":5,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/73447\/revisions"}],"predecessor-version":[{"id":128970,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/73447\/revisions\/128970"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=73447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=73447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=73447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}