{"id":1914,"date":"2020-07-10T19:27:01","date_gmt":"2020-07-10T23:27:01","guid":{"rendered":"http:\/\/shirishranjit.com\/blog1\/?page_id=1914"},"modified":"2020-07-10T19:57:10","modified_gmt":"2020-07-10T23:57:10","slug":"reactjs-component-from-controlled-to-uncontrolled-explained","status":"publish","type":"page","link":"https:\/\/shirishranjit.com\/blog1\/reactjs\/reactjs-component-from-controlled-to-uncontrolled-explained","title":{"rendered":"ReactJS component from controlled to uncontrolled explained"},"content":{"rendered":"\n<p>If you don&#8217;t set\u00a0<strong>initial state<\/strong>\u00a0react will treat that as an\u00a0<strong>uncontrolled component<\/strong><\/p>\n\n\n\n<p>There are a few things to note here. If you open your comment, you&#8217;ll notice two warning messages pop up immediately:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Warning:&nbsp;<code>value<\/code>&nbsp;prop on&nbsp;<code>input<\/code>&nbsp;should not be null. Consider using the empty string to clear the component or&nbsp;<code>undefined<\/code>&nbsp;for uncontrolled components.<\/p><p>Warning: FormPresenter contains an input of type text with both value and defaultValue props. Input elements must be either controlled or uncontrolled (specify either the value prop, or the defaultValue prop, but not both). Decide between using a controlled or uncontrolled input element and remove one of these props.<\/p><\/blockquote>\n\n\n\n<p>Putting the jsx here for reference:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;input \n    type=\"text\" \n     value={this.state.value} \n     defaultValue={this.state.defaultValue}\n     onChange={this.handleChange}\n  \/><\/code><\/pre>\n\n\n\n<p>The first warning is telling you there&#8217;s something wrong with what you&#8217;re passing as a value, and is why it gives you the error you&#8217;re asking about (more on this in a minute).<\/p>\n\n\n\n<p>The second warning is telling you that controlled components should only use&nbsp;<code>value<\/code>&nbsp;and uncontrolled components should only use&nbsp;<code>defaultValue<\/code>. The reason here is if you need to set a default value for a controlled component, you can just default the value you pass to&nbsp;<code>value<\/code>. And if you&#8217;re using an uncontrolled component you only care about setting a default value as you want the browser to manage the current value normally.<\/p>\n\n\n\n<p>So, now when you type in the input, it gives you that third warning:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Warning: FormPresenter is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component<\/p><\/blockquote>\n\n\n\n<p>This is linked to the first warning. Because you&#8217;re defaulting&nbsp;<code>this.state.value<\/code>&nbsp;to&nbsp;<code>null<\/code>, the&nbsp;<code>input<\/code>&nbsp;is not considered controlled. If you change the default to an empty string (<code>''<\/code>), you&#8217;re properly controlling the input and will not get the warning message.<\/p>\n<div class=\"twttr_buttons\"><div class=\"twttr_twitter\">\n\t\t\t\t\t<a href=\"http:\/\/twitter.com\/share?text=ReactJS+component+from+controlled+to+uncontrolled+explained\" class=\"twitter-share-button\" data-via=\"\" data-hashtags=\"\"  data-size=\"default\" data-url=\"https:\/\/shirishranjit.com\/blog1\/reactjs\/reactjs-component-from-controlled-to-uncontrolled-explained\"  data-related=\"\" target=\"_blank\">Tweet<\/a>\n\t\t\t\t<\/div><div class=\"twttr_followme\">\n\t\t\t\t\t\t<a href=\"https:\/\/twitter.com\/shiranjit\" class=\"twitter-follow-button\" data-size=\"default\"  data-show-screen-name=\"false\"  target=\"_blank\">Follow me<\/a>\n\t\t\t\t\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>If you don&#8217;t set\u00a0initial state\u00a0react will treat that as an\u00a0uncontrolled component There are a few things to note here. If you open your comment, you&#8217;ll notice two warning messages pop up immediately: Warning:&nbsp;value&nbsp;prop on&nbsp;input&nbsp;should not be null. Consider using the &hellip; <a href=\"https:\/\/shirishranjit.com\/blog1\/reactjs\/reactjs-component-from-controlled-to-uncontrolled-explained\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"parent":1918,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1914","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/1914"}],"collection":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/comments?post=1914"}],"version-history":[{"count":2,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/1914\/revisions"}],"predecessor-version":[{"id":1921,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/1914\/revisions\/1921"}],"up":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/1918"}],"wp:attachment":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/media?parent=1914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}