<?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>Forem: Saket23</title>
    <description>The latest articles on Forem by Saket23 (@saket23).</description>
    <link>https://forem.com/saket23</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%2F110073%2Fe61c7ccf-1ff3-405b-a336-35e4b3d3330e.png</url>
      <title>Forem: Saket23</title>
      <link>https://forem.com/saket23</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/saket23"/>
    <language>en</language>
    <item>
      <title>How to use same state for multiple controlled components in react hooks </title>
      <dc:creator>Saket23</dc:creator>
      <pubDate>Tue, 10 Mar 2020 18:52:42 +0000</pubDate>
      <link>https://forem.com/saket23/how-to-use-same-state-for-multiple-controlled-components-in-react-hooks-42co</link>
      <guid>https://forem.com/saket23/how-to-use-same-state-for-multiple-controlled-components-in-react-hooks-42co</guid>
      <description>&lt;p&gt;In React class component we can use same event handlers to update state for multiple controlled components using &lt;code&gt;event.target.name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class App extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      name: "",
      address: "",
      occupation: ""
    };
    this.handleEventChange = this.handleEventChange.bind(this);
  }

//Same event handler for all the three input field
  handleEventChange(event) {
    this.setState({ [event.target.name]: event.target.value });
  }

  render() {
    const { name, address, occupation } = this.state;
    return (
      &amp;lt;&amp;gt;
        &amp;lt;&amp;gt;
          Name:{" "}
          &amp;lt;input name="name" value={name} onChange={this.handleEventChange} /&amp;gt;
        &amp;lt;/&amp;gt;
        &amp;lt;&amp;gt;
          Address:{" "}
          &amp;lt;input
            name="address"
            value={address}
            onChange={this.handleEventChange}
          /&amp;gt;
        &amp;lt;/&amp;gt;
        &amp;lt;&amp;gt;
          Occupation:{" "}
          &amp;lt;input
            name="occupation"
            value={occupation}
            onChange={this.handleEventChange}
          /&amp;gt;
        &amp;lt;/&amp;gt;
      &amp;lt;/&amp;gt;
    );
  }
}

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

&lt;/div&gt;



&lt;p&gt;But in react hooks we might not be able to use the above mentioned way if we are using different state for each of the controlled component using 'useState'&lt;/p&gt;

&lt;p&gt;We can use the same state in hooks as well by using spread operator.&lt;/p&gt;

&lt;p&gt;create an initialState object with the name of all the controlled component and initialise it to ourState using useState&lt;/p&gt;

&lt;p&gt;As Below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const initialState = {
    name: "",
    address: "",
    occupation: ""
  };
  const [ourState, ourSetState] = useState(initialState);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the spread operator to update the state using single event handler&lt;br&gt;
As below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function handleEventChange(event) {
    ourSetState({ ...ourState, [event.target.name]: event.target.value });
  }

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

&lt;/div&gt;



&lt;p&gt;Hope this article helps in reducing some lines of code&lt;br&gt;
The full code for react hooks is below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  //initial state for our common state
  const initialState = {
    name: "",
    address: "",
    occupation: ""
  };

  //initialise our state
  const [ourState, ourSetState] = useState(initialState);

  const { name, address, occupation } = ourState;

  //common event handler for all the controlled components.
  //using spread operator to update the state
  function handleEventChange(event) {
    ourSetState({ ...ourState, [event.target.name]: event.target.value });
  }

  return (
    &amp;lt;&amp;gt;
      &amp;lt;&amp;gt;
        Name: &amp;lt;input name="name" value={name} onChange={handleEventChange} /&amp;gt;
      &amp;lt;/&amp;gt;
      &amp;lt;&amp;gt;
        Address:{" "}
        &amp;lt;input name="address" value={address} onChange={handleEventChange} /&amp;gt;
      &amp;lt;/&amp;gt;
      &amp;lt;&amp;gt;
        Occupation:{" "}
        &amp;lt;input
          name="occupation"
          value={occupation}
          onChange={handleEventChange}
        /&amp;gt;
      &amp;lt;/&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy Javascripting !!!&lt;/p&gt;

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