{"id":73445,"date":"2020-01-15T17:59:52","date_gmt":"2020-01-15T12:29:52","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=73445"},"modified":"2024-04-12T14:23:13","modified_gmt":"2024-04-12T08:53:13","slug":"when-to-use-state","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/","title":{"rendered":"When to use state?"},"content":{"rendered":"<p>When a component needs to keep track of information between renderings the component itself can create, update, and use state. We\u2019ll be working with a fairly simple component to see state working in action. We\u2019ve got a button that keeps track of how many times you\u2019ve clicked it.<\/p>\n<p>Here\u2019s the code:<\/p>\n<p>class Button extends React.Component {<\/p>\n<p>constructor() {<\/p>\n<p>super();<\/p>\n<p>this.state = {<\/p>\n<p>count: 0,<\/p>\n<p>};<\/p>\n<p>}<\/p>\n<p>updateCount() {<\/p>\n<p>this.setState((prevState, props) =&gt; {<\/p>\n<p>return { count: prevState.count + 1 }<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>render() {<\/p>\n<p>return (&lt;button<\/p>\n<p>onClick={() =&gt; this.updateCount()}<\/p>\n<p>&gt;<\/p>\n<p>Clicked {this.state.count} times<\/p>\n<p>&lt;\/button&gt;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>So now we\u2019re working with state things are a bit more complicated. But we\u2019ll break it down to make it more understandable.<\/p>\n<p>Our first real difference between props and state is, state is created in the component. Let\u2019s look at the constructor method:<\/p>\n<p>constructor() {<\/p>\n<p>super();<\/p>\n<p>this.state = {<\/p>\n<p>count: 0,<\/p>\n<p>};<\/p>\n<p>}<\/p>\n<p>This is where state gets it\u2019s initial data. The inital data can be hard coded (as above), but it can also come from props. Well that\u2019s just confusing. It is, I know. But it makes sense &#8211; you can\u2019t change props, but it\u2019s pretty reasonable to want to do stuff to the data that a component receives. That\u2019s where state comes in. Moving on brings us to our second difference, state is changeable<\/p>\n<p>Here\u2019s updateCount again:<\/p>\n<p>updateCount() {<\/p>\n<p>this.setState((prevState, props) =&gt; {<\/p>\n<p>return { count: prevState.count + 1 }<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>We change the state to keep track of the total number of clicks. The important bit is setState. First off, notice that setState takes a function, that\u2019s becuase setState can run asynchronously. It needs to take a callback function rather than updating the state directly. You can see we have access to prevState within the callback, this will contain the previous state, even if the state has already been updated somewhere else. Pretty slick, huh?<\/p>\n<p>But React goes one step better, setState updates the state object and re-renders the component automagically. Yeah, this is pretty great of React to do, no need for us to explicitly re-render or worry about anything. React will take care of it all!<\/p>\n<p>It is tempting to write this.state.count = this.state.count + 1. Do not do this! React cannot listen to the state getting updated in this way, so your component will not re-render. Always use setState. It might also be tempting to write something like this:<\/p>\n<p>\/\/ DO NOT USE<\/p>\n<p>this.setState({<\/p>\n<p>count: this.state.count + 1<\/p>\n<p>});<\/p>\n<p>Although this might look reasonable, doesn\u2019t throw errors, and you might find examples that use this syntax online, it is wrong. This does not take into account the asychronous nature that setState can use and might cause errors with out of sync state data.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When a component needs to keep track of information between renderings the component itself can create, update, and use state. We\u2019ll be working with a fairly simple component to see state working in action. We\u2019ve got a button that keeps track of how many times you\u2019ve clicked it. Here\u2019s the code: class Button extends React.Component&#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":[8528],"tags":[8564],"class_list":["post-73445","page","type-page","status-publish","hentry","category-reactjs","tag-when-to-use-state"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>When to use state? - 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\/when-to-use-state\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"When to use state? - Tutorial\" \/>\n<meta property=\"og:description\" content=\"When a component needs to keep track of information between renderings the component itself can create, update, and use state. We\u2019ll be working with a fairly simple component to see state working in action. We\u2019ve got a button that keeps track of how many times you\u2019ve clicked it. Here\u2019s the code: class Button extends React.Component...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/\" \/>\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=\"2 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\/when-to-use-state\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/\",\"name\":\"When to use state? - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-15T12:29:52+00:00\",\"dateModified\":\"2024-04-12T08:53:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"When to use state?\"}]},{\"@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":"When to use state? - 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\/when-to-use-state\/","og_locale":"en_US","og_type":"article","og_title":"When to use state? - Tutorial","og_description":"When a component needs to keep track of information between renderings the component itself can create, update, and use state. We\u2019ll be working with a fairly simple component to see state working in action. We\u2019ve got a button that keeps track of how many times you\u2019ve clicked it. Here\u2019s the code: class Button extends React.Component...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/","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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/","name":"When to use state? - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-15T12:29:52+00:00","dateModified":"2024-04-12T08:53:13+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/when-to-use-state\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"When to use state?"}]},{"@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\/73445","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=73445"}],"version-history":[{"count":3,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/73445\/revisions"}],"predecessor-version":[{"id":75255,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/73445\/revisions\/75255"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=73445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=73445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=73445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}