<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stanley </title>
    <description>The latest articles on DEV Community by Stanley  (@pierrelstan).</description>
    <link>https://dev.to/pierrelstan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F76859%2F29a790ed-51db-430b-b99f-420e9fbaac0d.png</url>
      <title>DEV Community: Stanley </title>
      <link>https://dev.to/pierrelstan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pierrelstan"/>
    <language>en</language>
    <item>
      <title>Difference between controlled components and uncontrolled components in Reactjs</title>
      <dc:creator>Stanley </dc:creator>
      <pubDate>Tue, 26 Apr 2022 01:30:49 +0000</pubDate>
      <link>https://dev.to/pierrelstan/difference-between-controlled-components-and-uncontrolled-components-in-reactjs-i7l</link>
      <guid>https://dev.to/pierrelstan/difference-between-controlled-components-and-uncontrolled-components-in-reactjs-i7l</guid>
      <description>&lt;h2&gt;
  
  
  I - &lt;strong&gt;Controlled components&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In controlled components, the form data is handled by React components, and it is recommended to use controlled components to implement forms.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react'

export default function ControlComponent() {
  const [state, setState] = useState({
    value: ''
    })

    const handleChange =(event)=&amp;gt; {
      setState({value: event.target.value});
     }

    const handleSubmit = (event)=&amp;gt; {
    alert('A name was submitted: ' + state.value);
    event.preventDefault();
    }

    return (
    &amp;lt;form onSubmit={handleSubmit}&amp;gt;
            &amp;lt;label&amp;gt;
              Name:
              &amp;lt;input type="text" value={state.value} onChange={handleChange} /&amp;gt;
            &amp;lt;/label&amp;gt;
            &amp;lt;input type="submit" value="Submit" /&amp;gt;
          &amp;lt;/form&amp;gt;
    );

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  II- &lt;strong&gt;Uncontrolled components&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In uncontrolled components, the form data is handled by the DOM itself.&lt;br&gt;
Instead of writing an event handler for every state update, you can use &lt;strong&gt;useRef&lt;/strong&gt; to get form values from the DOM.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {useRef} from 'react'

export default function UnControlledComponent() {
  const inputElement = useRef(null);

  const handleSubmit =(event)=&amp;gt; {
     alert('A name was submitted: ' + inputElement.current.value);
     event.preventDefault();
   }

     return (
       &amp;lt;form onSubmit={handleSubmit}&amp;gt;
         &amp;lt;label&amp;gt;
           Name:
           &amp;lt;input type="text" ref={inputElement} /&amp;gt;
         &amp;lt;/label&amp;gt;
         &amp;lt;input type="submit" value="Submit" /&amp;gt;
       &amp;lt;/form&amp;gt;
     );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
