<?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: Hayk Safaryan</title>
    <description>The latest articles on Forem by Hayk Safaryan (@hayk94).</description>
    <link>https://forem.com/hayk94</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%2F113818%2F12eeb797-aeba-4db9-8a7b-72ecb12e14b5.png</url>
      <title>Forem: Hayk Safaryan</title>
      <link>https://forem.com/hayk94</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hayk94"/>
    <language>en</language>
    <item>
      <title>React Hooks Re-intro </title>
      <dc:creator>Hayk Safaryan</dc:creator>
      <pubDate>Sun, 07 Apr 2019 21:35:21 +0000</pubDate>
      <link>https://forem.com/hayk94/react-hooks-re-intro-77m</link>
      <guid>https://forem.com/hayk94/react-hooks-re-intro-77m</guid>
      <description>&lt;h1&gt;
  
  
  React Hooks Re-intro
&lt;/h1&gt;

&lt;p&gt;The original post is in this repo &lt;br&gt;
&lt;a href="https://github.com/hayk94/react-hooks-intro-coffeeshop" rel="noopener noreferrer"&gt;https://github.com/hayk94/react-hooks-intro-coffeeshop&lt;/a&gt;&lt;br&gt;&lt;br&gt;
where each branch has a readme.md explaining over the code.&lt;/p&gt;

&lt;p&gt;This repository is for React Hooks introduction.&lt;/p&gt;

&lt;p&gt;It consists of several numbered branches.&lt;br&gt;&lt;br&gt;
In each branch readme file goes over the code,&lt;br&gt;&lt;br&gt;
explaining the advantages and caveats.&lt;/p&gt;

&lt;p&gt;Here in the master branch is a plain &lt;a href="https://facebook.github.io/create-react-app/" rel="noopener noreferrer"&gt;CRA&lt;/a&gt; app.&lt;br&gt;&lt;br&gt;
There are some additional configs for eslint using &lt;a href="https://www.npmjs.com/package/eslint-config-fbjs" rel="noopener noreferrer"&gt;eslint-config-fbjs&lt;/a&gt;&lt;br&gt;&lt;br&gt;
with &lt;a href="https://www.npmjs.com/package/eslint-plugin-react-hooks" rel="noopener noreferrer"&gt;eslint-plugin-react-hooks&lt;/a&gt;.&lt;br&gt;
And prettier config.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;Most stuff in this repo and posts have already been discussed,&lt;br&gt;&lt;br&gt;
by react team in the docs, by Dan Abramov in his amazing talk and blog,&lt;br&gt;&lt;br&gt;
and other people.&lt;/p&gt;

&lt;p&gt;This repo is just a summation of my knowledge about hooks I gathered so far.&lt;br&gt;
Just putting stuff in my own words.&lt;/p&gt;
&lt;h6&gt;
  
  
  Resources
&lt;/h6&gt;

&lt;p&gt;&lt;a href=""&gt;https://reactjs.org/docs/hooks-intro.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/dan_abramov/making-sense-of-react-hooks-2eib"&gt;https://dev.to/dan_abramov/making-sense-of-react-hooks-2eib&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://overreacted.io/react-as-a-ui-runtime/" rel="noopener noreferrer"&gt;https://overreacted.io/react-as-a-ui-runtime/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://overreacted.io/making-setinterval-declarative-with-react-hooks/" rel="noopener noreferrer"&gt;https://overreacted.io/making-setinterval-declarative-with-react-hooks/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://overreacted.io/how-are-function-components-different-from-classes/" rel="noopener noreferrer"&gt;https://overreacted.io/how-are-function-components-different-from-classes/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://overreacted.io/a-complete-guide-to-useeffect/" rel="noopener noreferrer"&gt;https://overreacted.io/a-complete-guide-to-useeffect/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://overreacted.io/writing-resilient-components/" rel="noopener noreferrer"&gt;https://overreacted.io/writing-resilient-components/&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  App Intro
&lt;/h2&gt;

&lt;p&gt;Imagine we are writing a coffeeshop menu app.&lt;br&gt;&lt;br&gt;
Users can choose the product they want and order it.&lt;/p&gt;
&lt;h4&gt;
  
  
  Good Old Classes
&lt;/h4&gt;

&lt;p&gt;First we make a&lt;code&gt;Menu.js&lt;/code&gt; component.&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, {Component} from 'react';

class Menu extends Component {
  render() {
    return (
      &amp;lt;div&amp;gt;
        Menu
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;And make it render in our &lt;code&gt;App.js&lt;/code&gt; component.&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, { Component } from 'react';
import './App.css';
import Menu from './Menu';

class App extends Component {
  render() {
    return (
      &amp;lt;div className="App"&amp;gt;
        &amp;lt;section&amp;gt;
          &amp;lt;Menu/&amp;gt;
        &amp;lt;/section&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default App;

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

&lt;/div&gt;



&lt;p&gt;Then we add a &lt;code&gt;select&lt;/code&gt; to our menu app, with some products.&lt;br&gt;&lt;br&gt;
Add a button for ordering.&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, {Component} from 'react';

class Menu extends Component {
  render() {
      return (
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Order: &amp;lt;/b&amp;gt;
          &amp;lt;select&amp;gt;
            &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
            &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
            &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
          &amp;lt;div&amp;gt;
            &amp;lt;button&amp;gt;Order&amp;lt;/button&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
      );
    }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;Apparently this is what they sell at coffeeshops, isn't it?&lt;/p&gt;

&lt;p&gt;Now we need some state for the selected product.&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, {Component} from 'react';

class Menu extends Component {
  constructor(props) {
      super(props);
      this.state = {
        selected: 'Purple Haze',
      };
  }

  render() {
      return (
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Order: &amp;lt;/b&amp;gt;
          &amp;lt;select&amp;gt;
            &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
            &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
            &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
          &amp;lt;div&amp;gt;
            &amp;lt;button&amp;gt;Order&amp;lt;/button&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
      );
    }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;Finally we need methods to select the product and order it.&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, {Component} from 'react';

class Menu extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: 'Purple Haze',
    };
  }

  onChange(e) {
    this.setState({selected: e.target.value});
  }

  onOrder() {
    alert(`You ordered ${this.state.selected}`);
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Order: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={this.onChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;button onClick={this.onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;And here we made our app that's it.&lt;br&gt;&lt;br&gt;
Now lets go and order some stuff.  &lt;/p&gt;

&lt;p&gt;And...&lt;/p&gt;

&lt;p&gt;Oops...&lt;/p&gt;

&lt;p&gt;Right...&lt;/p&gt;

&lt;p&gt;We get a big nice error message, guess what?&lt;/p&gt;

&lt;p&gt;Turn to the next branch...&lt;/p&gt;
&lt;h2&gt;
  
  
  And now this!
&lt;/h2&gt;

&lt;p&gt;So now we have an error! Or rather the error...&lt;/p&gt;

&lt;p&gt;You know it right?&lt;/p&gt;

&lt;p&gt;Let's fix it!&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, {Component} from 'react';

class Menu extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: 'Purple Haze',
    };
    this.onChange = this.onChange.bind(this);
    this.onOrder = this.onOrder.bind(this);
  }

  onChange(e) {
    this.setState({selected: e.target.value});
  }

  onOrder() {
    alert(`You ordered ${this.state.selected}`);
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Order: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={this.onChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;button onClick={this.onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;We needed to &lt;code&gt;bind&lt;/code&gt; &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  New Requirements
&lt;/h3&gt;

&lt;p&gt;Suddenly we got new requirements from the client...&lt;br&gt;&lt;br&gt;
They want the page title to be the selected item of the user.&lt;/p&gt;

&lt;p&gt;So we need something like this.&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, {Component} from 'react';

class Menu extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: 'Purple Haze',
    };
    this.onChange = this.onChange.bind(this);
    this.onOrder = this.onOrder.bind(this);
  }

  componentDidUpdate() {
    document.title = `Selected - ${this.state.selected}`;
  }

  onChange(e) {
    this.setState({selected: e.target.value});
  }

  onOrder() {
    alert(`You ordered ${this.state.selected}`);
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Order: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={this.onChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;button onClick={this.onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;Lifecycle methods yay!&lt;br&gt;&lt;br&gt;
Now we got it once user selects an item the document title changes accordingly.&lt;/p&gt;
&lt;h3&gt;
  
  
  Yet another requirement
&lt;/h3&gt;

&lt;p&gt;And now we've been given another task based on the new requirements.&lt;br&gt;
Users should be able to tell us how many products they want to order.&lt;/p&gt;

&lt;p&gt;Pretty easy, right?&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, {Component} from 'react';

class Menu extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: 'Purple Haze',
      count: 0,
    };

    this.onProductChange = this.onProductChange.bind(this);
    this.onOrder = this.onOrder.bind(this);
    this.onCountChange = this.onCountChange.bind(this);
  }

  componentDidUpdate() {
    document.title = `Selected - ${this.state.selected}`;
  }

  onProductChange(e) {
    this.setState({selected: e.target.value});
  }

  onOrder() {
    alert(`You ordered ${this.state.count} ${this.state.selected}`);
  }

  onCountChange(e) {
    this.setState({count: e.target.value});
  }

  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
          &amp;lt;select onChange={this.onProductChange}&amp;gt;
            &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
            &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
            &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
          &amp;lt;input
            type="number"
            min={0}
            value={this.state.count}
            onChange={this.onCountChange}
          /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;button onClick={this.onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;We add a number &lt;code&gt;input&lt;/code&gt;, &lt;code&gt;count&lt;/code&gt; to our state, and an &lt;code&gt;onCountChange&lt;/code&gt; method.&lt;br&gt;&lt;br&gt;
Oh and right, we need to &lt;code&gt;bind&lt;/code&gt; &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Great we accomplished a lot today and feel proud.&lt;/p&gt;
&lt;h3&gt;
  
  
  Oh no, a bug was just reported
&lt;/h3&gt;

&lt;p&gt;Whoops... We just barely finished the previous task,&lt;br&gt;&lt;br&gt;
yet a bug was reported from our previous feature.&lt;/p&gt;

&lt;p&gt;They say when the users enter the page first time,&lt;br&gt;
the page title doesn't show the selected product.&lt;/p&gt;

&lt;p&gt;But it's not a bug! The user didn't select any product yet!&lt;br&gt;&lt;br&gt;
Oh really? It's a bug and you should fix it!&lt;/p&gt;

&lt;p&gt;Anyway it needs to be done.&lt;br&gt;&lt;br&gt;
So after thinking a while, the best place for this would be &lt;code&gt;componentDidMount&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Yay another lifecycle method to the rescue!&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, {Component} from 'react';

class Menu extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: 'Purple Haze',
      count: 0,
    };

    this.onProductChange = this.onProductChange.bind(this);
    this.onOrder = this.onOrder.bind(this);
    this.onCountChange = this.onCountChange.bind(this);
  }

  componentDidMount() {
    document.title = `Selected - ${this.state.selected}`;
  }

  componentDidUpdate() {
    document.title = `Selected - ${this.state.selected}`;
  }

  onProductChange(e) {
    this.setState({selected: e.target.value});
  }

  onOrder() {
    alert(`You ordered ${this.state.count} ${this.state.selected}`);
  }

  onCountChange(e) {
    this.setState({count: e.target.value});
  }

  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
          &amp;lt;select onChange={this.onProductChange}&amp;gt;
            &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
            &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
            &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
          &amp;lt;input
            type="number"
            min={0}
            value={this.state.count}
            onChange={this.onCountChange}
          /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;button onClick={this.onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;Phew... it's been a tough day, but we managed! Hooray!&lt;/p&gt;

&lt;p&gt;Turn to the next branch...&lt;/p&gt;

&lt;h3&gt;
  
  
  App grows! Performance issues come up!
&lt;/h3&gt;

&lt;p&gt;As our app grows, we are encountering some performance issues here and there.&lt;/p&gt;

&lt;p&gt;To identify them we start using some debugging tools.&lt;br&gt;&lt;br&gt;
So you decide to put some loggers in your &lt;code&gt;Menu.js&lt;/code&gt;.&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, {Component} from 'react';

class Menu extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: 'Purple Haze',
      count: 0,
    };

    this.onProductChange = this.onProductChange.bind(this);
    this.onOrder = this.onOrder.bind(this);
    this.onCountChange = this.onCountChange.bind(this);
  }

  componentDidMount() {
    // eslint-disable-next-line
    console.log('logger', this.state, this.props);
    document.title = `Selected - ${this.state.selected}`;
  }

  componentDidUpdate() {
    // eslint-disable-next-line
    console.log('logger', this.state, this.props);
    document.title = `Selected - ${this.state.selected}`;
  }

  onProductChange(e) {
    this.setState({selected: e.target.value});
  }

  onOrder() {
    alert(`You ordered ${this.state.count} ${this.state.selected}`);
  }

  onCountChange(e) {
    this.setState({count: e.target.value});
  }

  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
          &amp;lt;select onChange={this.onProductChange}&amp;gt;
            &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
            &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
            &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
          &amp;lt;input
            type="number"
            min={0}
            value={this.state.count}
            onChange={this.onCountChange}
          /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;button onClick={this.onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;So we put loggers in &lt;code&gt;componentDidUpdate&lt;/code&gt; and &lt;code&gt;componentDidMount&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Now you see the proper log after &lt;code&gt;componentDidMount&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
You select a product and see the proper log in &lt;code&gt;componentDidUpdate&lt;/code&gt;, with new state and props.&lt;br&gt;&lt;br&gt;
You change the product count and see new state and props logged by &lt;code&gt;componentDidUpdate&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;But wait a minute...&lt;br&gt;&lt;br&gt;
Doesn't that mean the &lt;code&gt;document.title = newTitle&lt;/code&gt; code executes on every update,&lt;br&gt;&lt;br&gt;
even though the selected product didn't change?&lt;/p&gt;

&lt;p&gt;We need to fix that. And this logger tool is really helpful we should implement it for other components too in our app.&lt;/p&gt;

&lt;p&gt;So maybe we fix the issue with an &lt;code&gt;if&lt;/code&gt; check. And make a &lt;code&gt;HOC&lt;/code&gt; for the logger.&lt;/p&gt;

&lt;p&gt;As you are thinking about the solution a new high priority requirement arrives.&lt;/p&gt;

&lt;p&gt;Turn to the next branch...&lt;/p&gt;
&lt;h3&gt;
  
  
  New Requirement with lots async stuff
&lt;/h3&gt;

&lt;p&gt;So suddenly a new requirement arrives.&lt;br&gt;&lt;br&gt;
To implement it you added lots of async stuff to your on order function, so now it looks like this.&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, {Component} from 'react';

class Menu extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: 'Purple Haze',
      count: 0,
    };

    this.onProductChange = this.onProductChange.bind(this);
    this.onOrder = this.onOrder.bind(this);
    this.onCountChange = this.onCountChange.bind(this);
  }

  componentDidMount() {
    // eslint-disable-next-line
    console.log('logger', this.state, this.props);
    document.title = `Selected - ${this.state.selected}`;
  }

  componentDidUpdate() {
    // eslint-disable-next-line
    console.log('logger', this.state, this.props);
    document.title = `Selected - ${this.state.selected}`;
  }

  onProductChange(e) {
    this.setState({selected: e.target.value});
  }

  onOrder() {
    setTimeout(() =&amp;gt; {
      alert(`You ordered ${this.state.count} ${this.state.selected}`);
    }, 3000);
  }

  onCountChange(e) {
    this.setState({count: e.target.value});
  }

  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
          &amp;lt;select onChange={this.onProductChange}&amp;gt;
            &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
            &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
            &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
          &amp;lt;input
            type="number"
            min={0}
            value={this.state.count}
            onChange={this.onCountChange}
          /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;button onClick={this.onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;Everything seems to be working just fine, but then you get a bug report.&lt;br&gt;&lt;br&gt;
Wrong item is being ordered sometimes.  &lt;/p&gt;

&lt;p&gt;After trying to reproduce it for a while, you find the problem!&lt;/p&gt;

&lt;p&gt;When you order a product then select another product before the order message appeared,&lt;br&gt;&lt;br&gt;
you get a wrong product in the message as it appears.&lt;/p&gt;

&lt;p&gt;Hmmm...&lt;br&gt;
Why that would happen? Nothing seems to be wrong with our code.&lt;/p&gt;

&lt;p&gt;Turn to the next branch...&lt;/p&gt;
&lt;h3&gt;
  
  
  Ugh not this again!
&lt;/h3&gt;

&lt;p&gt;So why that bug happens?&lt;/p&gt;

&lt;p&gt;To give you a hint, lets take a look at the solution first.&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, {Component} from 'react';

class Menu extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: 'Purple Haze',
      count: 0,
    };

    this.onProductChange = this.onProductChange.bind(this);
    this.onOrder = this.onOrder.bind(this);
    this.onCountChange = this.onCountChange.bind(this);
  }

  componentDidMount() {
    // eslint-disable-next-line
    console.log('logger', this.state, this.props);
    document.title = `Selected - ${this.state.selected}`;
  }

  componentDidUpdate() {
    // eslint-disable-next-line
    console.log('logger', this.state, this.props);
    document.title = `Selected - ${this.state.selected}`;
  }

  onProductChange(e) {
    this.setState({selected: e.target.value});
  }

  onOrder() {
    const {count, selected} = this.state;
    setTimeout(() =&amp;gt; {
      alert(`You ordered ${count} ${selected}`);
    }, 3000);
  }

  onCountChange(e) {
    this.setState({count: e.target.value});
  }

  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
          &amp;lt;select onChange={this.onProductChange}&amp;gt;
            &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
            &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
            &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
          &amp;lt;input
            type="number"
            min={0}
            value={this.state.count}
            onChange={this.onCountChange}
          /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;button onClick={this.onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Menu;

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

&lt;/div&gt;



&lt;p&gt;So we only changed this piece.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;onOrder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`You ordered &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just one line of code fixes our issue.&lt;/p&gt;

&lt;p&gt;Let's dive deep and understand what happens here.&lt;/p&gt;

&lt;h4&gt;
  
  
  The "this" is mutable
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;onOrder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`You ordered &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a look at this buggy code first.&lt;/p&gt;

&lt;p&gt;We select a product - "GoGreen". State changes to it. Component re-renders.&lt;br&gt;&lt;br&gt;
&lt;code&gt;this.state.selected === "GoGreen"&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Click the order button. The &lt;code&gt;onOrder&lt;/code&gt; method fires.&lt;br&gt;&lt;br&gt;
&lt;code&gt;setTimeout&lt;/code&gt; starts. 3 seconds pass. The callback is executed.&lt;br&gt;&lt;br&gt;
We read &lt;code&gt;this.state.selected&lt;/code&gt; and get "GoGreen".&lt;/p&gt;

&lt;p&gt;Here everything is working great. Now let's see how the bug happens.&lt;/p&gt;

&lt;p&gt;We select a product - "Amnesia". State changes to it. Component re-renders.&lt;br&gt;&lt;br&gt;
&lt;code&gt;this.state.selected === "Amnesia"&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Click the order button. The &lt;code&gt;onOrder&lt;/code&gt; method fires.&lt;br&gt;&lt;br&gt;
&lt;code&gt;setTimeout&lt;/code&gt; starts. &lt;strong&gt;Before&lt;/strong&gt; 3 seconds pass, we select another product - "GoGreen".&lt;br&gt;&lt;br&gt;
State changes to it. Component re-renders.&lt;br&gt;&lt;br&gt;
&lt;code&gt;this.state.selected === "GoGreen"&lt;/code&gt;&lt;br&gt;&lt;br&gt;
3 seconds pass. &lt;code&gt;setTimeout&lt;/code&gt; callback runs. We read &lt;code&gt;this.state.selected&lt;/code&gt; and get "GoGreen".&lt;br&gt;&lt;br&gt;
However this time we clicked the order button when we selected the "Amnesia" product.&lt;br&gt;&lt;br&gt;
The problem here is the &lt;code&gt;this&lt;/code&gt;. It changes during the scope of the &lt;code&gt;onOrder&lt;/code&gt;.&lt;/p&gt;
&lt;h5&gt;
  
  
  Solution
&lt;/h5&gt;

&lt;p&gt;Now let's take a look at the solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="nf"&gt;onOrder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`You ordered &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We select a product - "Amnesia". State changes to it. Component re-renders.&lt;br&gt;&lt;br&gt;
 &lt;code&gt;this.state.selected === "Amnesia"&lt;/code&gt;&lt;br&gt;&lt;br&gt;
 Click the order button. The &lt;code&gt;onOrder&lt;/code&gt; method fires. &lt;br&gt;
 We read the &lt;code&gt;this.state.selected&lt;/code&gt; and assign it to the new variable &lt;code&gt;selected&lt;/code&gt; in the function scope.&lt;br&gt;&lt;br&gt;
 &lt;code&gt;selected === "Amnesia"&lt;/code&gt; &lt;br&gt;
 &lt;code&gt;setTimeout&lt;/code&gt; starts. &lt;strong&gt;Before&lt;/strong&gt; 3 seconds pass, we select another product - "GoGreen".&lt;br&gt;&lt;br&gt;
 State changes to it. Component re-renders.&lt;br&gt;&lt;br&gt;
 &lt;code&gt;this.state.selected === "GoGreen"&lt;/code&gt;&lt;br&gt;&lt;br&gt;
 3 seconds pass. &lt;code&gt;setTimeout&lt;/code&gt; callback runs. We read the &lt;code&gt;selected&lt;/code&gt; of the function scope not from the &lt;code&gt;this&lt;/code&gt;. And get "Amnesia".&lt;br&gt;&lt;br&gt;
The &lt;code&gt;this&lt;/code&gt; changed/mutated but function scope and variables in it were still the same.&lt;/p&gt;

&lt;p&gt;So we solved the &lt;code&gt;this&lt;/code&gt; problem by function scope.&lt;/p&gt;
&lt;h4&gt;
  
  
  2 Dimensions
&lt;/h4&gt;

&lt;p&gt;One way that I find easy to think about the &lt;code&gt;this&lt;/code&gt; and function scope, is to think about them like dimensions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7qq9r27o14s07ih1g0h9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7qq9r27o14s07ih1g0h9.png" alt="dimensions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have these 2 dimensions where we store our data.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; can change during the scope.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiwdpitelugmkujkvzlp1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiwdpitelugmkujkvzlp1.png" alt="dimensions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So you need to be aware of the 2 dimensions where our data reside. And how they interact. &lt;/p&gt;
&lt;h3&gt;
  
  
  Fucking bring hooks already!
&lt;/h3&gt;

&lt;p&gt;Fine! Fine...&lt;/p&gt;

&lt;p&gt;So you heard about this next hot thing that's called hooks.&lt;br&gt;&lt;br&gt;
Now you want to refactor your &lt;code&gt;Menu.js&lt;/code&gt; component to use hooks.&lt;br&gt;&lt;br&gt;
You were going to refactor it anyway because of performance issues in branch 3, so lets refactor straight to hooks.&lt;/p&gt;

&lt;p&gt;Create a new simple functional component &lt;code&gt;MenuFC.js&lt;/code&gt;.&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 from 'react';

const MenuFc = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So plain and beautiful. Simple function that returns some jsx.  &lt;/p&gt;

&lt;p&gt;Now what do you think, wouldn't it be nice if we could do something like this?&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 from 'react';

const MenuFc = () =&amp;gt; {
  const state = 'Purple Haze';

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select value={state}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wow, our state to be a simple variable in function scope. That's crazy man!  &lt;/p&gt;

&lt;p&gt;But we need to somehow be able to change it, right? Otherwise it's not useful as state.&lt;br&gt;&lt;br&gt;
Imagine if we had &lt;code&gt;setState&lt;/code&gt; as simple function in scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;select onChange={setState} value={state}&amp;gt;
  &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
  &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
  &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you think? It's so nice and clean.&lt;br&gt;&lt;br&gt;
So how do we accomplish this with hooks?&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';

const MenuFc = () =&amp;gt; {
  const [selected, setSelected] = useState('Purple Haze');
  const onProductChange = (e) =&amp;gt; {
    setSelected(e.target.value);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={onProductChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;

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

&lt;/div&gt;



&lt;p&gt;I know what you are thinking. "I liked you at first, but now, what kind of black magic is this?"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSelected&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Purple Haze&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please just give me a moment. It is a simple &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Array_destructuring" rel="noopener noreferrer"&gt;ES6 Array Destructuring&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
You learnt the big and verbose class syntax, surely this tiny syntax won't hurt you.&lt;/p&gt;
&lt;h4&gt;
  
  
  Now let's look at benefits
&lt;/h4&gt;

&lt;p&gt;Our &lt;code&gt;selected&lt;/code&gt; and &lt;code&gt;setSelected&lt;/code&gt; are just plain variables in our function scope.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; function imported from &lt;code&gt;react&lt;/code&gt; gets one argument - the initial state.&lt;br&gt;
It returns an array.&lt;br&gt;&lt;br&gt;
The first element is the value of our state.&lt;br&gt;&lt;br&gt;
The second is a function to change the value.  &lt;/p&gt;

&lt;p&gt;We use that function to create a callback to execute when a user selects different product.&lt;/p&gt;

&lt;p&gt;So take a look at this beauty once more.&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';

const MenuFc = () =&amp;gt; {
  const [selected, setSelected] = useState('Purple Haze');
  const onProductChange = (e) =&amp;gt; {
    setSelected(e.target.value);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select value={selected} onChange={onProductChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;

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

&lt;/div&gt;



&lt;p&gt;"But you are creating a new callback function on each render, it's bad for performance!".&lt;br&gt;&lt;br&gt;
Some of you might say.&lt;br&gt;&lt;br&gt;
Well it turns out... &lt;a href="https://reactjs.org/docs/hooks-faq.html#are-hooks-slow-because-of-creating-functions-in-render" rel="noopener noreferrer"&gt;No.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-faq.html#are-hooks-slow-because-of-creating-functions-in-render" rel="noopener noreferrer"&gt;Here is what react official docs say about that.&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;No. In modern browsers, the raw performance of closures compared to classes doesn’t differ significantly except in extreme scenarios&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Moreover&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hooks avoid a lot of the overhead that classes require, like the cost of creating class instances and binding event handlers in the constructor.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So if we just separate out politics from these sentences, the raw meaning strictly equals  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;As React components classes do so much shit that if we just throw them away and use functions,&lt;br&gt;&lt;br&gt;
we get a lot of performance benefit compared to which creating a new callback function at every render at most cases is nothing&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And you can find some performance tips and tricks mentioned there. We'll dive into these later.&lt;/p&gt;

&lt;p&gt;Now let's add the ordering functionality.&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';

const MenuFc = () =&amp;gt; {
  const [selected, setSelected] = useState('Purple Haze');
  const onProductChange = (e) =&amp;gt; {
    setSelected(e.target.value);
  };

  const onOrder = () =&amp;gt; {
      setTimeout(() =&amp;gt; {
        alert(`You ordered ${count} ${selected}`);
      }, 3000);
    };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select value={selected} onChange={onProductChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;

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

&lt;/div&gt;



&lt;p&gt;Again. Simple as that, just a plain function.&lt;br&gt;&lt;br&gt;
Notice in this case we don't need to put the &lt;code&gt;onOrder&lt;/code&gt; in the component scope.&lt;br&gt;&lt;br&gt;
We can declare it outside and use it inside the component by passing arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`You ordered &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here is another great thing about hooks. You can have as many of them as you'd like.&lt;br&gt;&lt;br&gt;
Let's implement the count.&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';

const MenuFc = () =&amp;gt; {
  const [selected, setSelected] = useState('Purple Haze');
  const onProductChange = (e) =&amp;gt; {
    setSelected(e.target.value);
  };

  const [count, setCount] = useState(0);
  const onCountChange = (e) =&amp;gt; {
    setCount(e.target.value);
  };

  const onOrder = () =&amp;gt; {
    setTimeout(() =&amp;gt; {
      alert(`You ordered ${count} ${selected}`);
    }, 3000);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={onProductChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
        &amp;lt;input
          type="number"
          min={0}
          value={count}
          onChange={onCountChange}
        /&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;

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

&lt;/div&gt;



&lt;p&gt;What a beauty! And we don't have any &lt;code&gt;this&lt;/code&gt; problems.&lt;br&gt;&lt;br&gt;
No bindings! No async bugs! Now it's just a plain function and only a scope.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8cg7l5y9nm2qtj4anxof.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8cg7l5y9nm2qtj4anxof.png" alt="One Dimension Only"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's get acquainted with the next hook.&lt;br&gt;
Turn to the next branch.&lt;/p&gt;
&lt;h3&gt;
  
  
  Side effects!
&lt;/h3&gt;

&lt;p&gt;Where did we perform side effects in class components?&lt;br&gt;&lt;br&gt;
Lifecycle methods.&lt;/p&gt;

&lt;p&gt;To perform side effects in functional components now we have &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;Menu.js&lt;/code&gt; class component we have a side effect for changing our document title.&lt;br&gt;&lt;br&gt;
Here is how we do it in &lt;code&gt;MenuFC.js&lt;/code&gt;&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, useEffect} from 'react';

const MenuFc = () =&amp;gt; {
  const [selected, setSelected] = useState('Purple Haze');
  const onProductChange = e =&amp;gt; {
    setSelected(e.target.value);
  };

  const [count, setCount] = useState(0);
  const onCountChange = e =&amp;gt; {
    setCount(e.target.value);
  };

  const onOrder = () =&amp;gt; {
    setTimeout(() =&amp;gt; {
      alert(`You ordered ${count} ${selected}`);
    }, 3000);
  };

  useEffect(() =&amp;gt; {
    document.title = `Selected - ${selected}`;
  });

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={onProductChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
        &amp;lt;input type="number" min={0} value={count} onChange={onCountChange} /&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; accepts a function as an argument.&lt;br&gt;&lt;br&gt;
There we perform our side effects.&lt;br&gt;&lt;br&gt;
It runs on every render.&lt;br&gt;
It behaves&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;as componentDidMount, componentDidUpdate, and componentWillUnmount combined.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;https://reactjs.org/docs/hooks-effect.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We also had a logger in our class component. Let's add it.&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, useEffect} from 'react';

const MenuFc = () =&amp;gt; {
  const [selected, setSelected] = useState('Purple Haze');
  const onProductChange = e =&amp;gt; {
    setSelected(e.target.value);
  };

  const [count, setCount] = useState(0);
  const onCountChange = e =&amp;gt; {
    setCount(e.target.value);
  };

  const onOrder = () =&amp;gt; {
    setTimeout(() =&amp;gt; {
      alert(`You ordered ${count} ${selected}`);
    }, 3000);
  };

  useEffect(() =&amp;gt; {
    // eslint-disable-next-line
    console.log('logger', selected, count);
    document.title = `Selected - ${selected}`;
  });

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={onProductChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
        &amp;lt;input type="number" min={0} value={count} onChange={onCountChange} /&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;

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

&lt;/div&gt;



&lt;p&gt;Everything looks great right? Before we go deeper one thing...&lt;/p&gt;

&lt;h3&gt;
  
  
  useEffect is a whole new concept
&lt;/h3&gt;

&lt;p&gt;Do not think of &lt;code&gt;useEffect&lt;/code&gt; as a new lifecycle method.&lt;br&gt;&lt;br&gt;
It's a whole new concept.&lt;/p&gt;

&lt;p&gt;It's not a lifecycle method, it behaves similar to them.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; timing is different.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-reference.html#timing-of-effects" rel="noopener noreferrer"&gt;https://reactjs.org/docs/hooks-reference.html#timing-of-effects&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Old problems
&lt;/h5&gt;

&lt;p&gt;In our class component we had a few issues.&lt;br&gt;&lt;br&gt;
As state changes our component re-renders.&lt;br&gt;&lt;br&gt;
&lt;code&gt;componentDidUpdate&lt;/code&gt; fires and &lt;code&gt;document.title = this.state.selected&lt;/code&gt; was running,&lt;br&gt;&lt;br&gt;
even though we changed only the count and not the title. With classes we'd put some if check.&lt;/p&gt;

&lt;p&gt;Also we wanted to make our logger functionality reusable. With classes we'd make a HOC.&lt;/p&gt;

&lt;p&gt;The same problems we have now here with our &lt;code&gt;useEffect&lt;/code&gt; hook.&lt;br&gt;&lt;br&gt;
At the moment it's as bad as lifecycle methods.&lt;/p&gt;

&lt;p&gt;Let's see how it's actually better.&lt;br&gt;&lt;br&gt;
Turn to the next branch.&lt;/p&gt;
&lt;h3&gt;
  
  
  It's useEffect not effects
&lt;/h3&gt;

&lt;p&gt;Take a look at this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    // eslint-disable-next-line
    console.log('logger', selected, count);
    document.title = `Selected - ${selected}`;
});

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

&lt;/div&gt;



&lt;p&gt;While it may seem okay, conceptually it is not.&lt;/p&gt;

&lt;p&gt;In the callback function, the 2 lines of code have different concerns.&lt;br&gt;&lt;br&gt;
They do stuff unrelated to each other.&lt;br&gt;&lt;br&gt;
We need better separation of concerns.&lt;/p&gt;

&lt;p&gt;So in fact this is much more better.&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, useEffect} from 'react';

const MenuFc = () =&amp;gt; {
  const [selected, setSelected] = useState('Purple Haze');
  const onProductChange = e =&amp;gt; {
    setSelected(e.target.value);
  };

  const [count, setCount] = useState(0);
  const onCountChange = e =&amp;gt; {
    setCount(e.target.value);
  };

  const onOrder = () =&amp;gt; {
    setTimeout(() =&amp;gt; {
      alert(`You ordered ${count} ${selected}`);
    }, 3000);
  };

  useEffect(() =&amp;gt; {
    document.title = `Selected - ${selected}`;
  });

  useEffect(() =&amp;gt; {
    // eslint-disable-next-line
    console.log('logger', selected, count);
  });

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={onProductChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
        &amp;lt;input type="number" min={0} value={count} onChange={onCountChange} /&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deps
&lt;/h3&gt;

&lt;p&gt;Now we need our &lt;code&gt;document.title&lt;/code&gt; effect run only when the &lt;code&gt;selected&lt;/code&gt; state changed.&lt;br&gt;&lt;br&gt;
In usual &lt;code&gt;componentDidUpdate&lt;/code&gt; you'd do some &lt;code&gt;prevProps&lt;/code&gt; comparisons and so on.&lt;/p&gt;

&lt;p&gt;Guess what? &lt;code&gt;useEffect&lt;/code&gt; now will do it for you! You just need to tell it what it needs to check.&lt;br&gt;&lt;br&gt;
How do we tell it what variables to check? Just pass a second argument to it.&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, useEffect} from 'react';

const MenuFc = () =&amp;gt; {
  const [selected, setSelected] = useState('Purple Haze');
  const onProductChange = e =&amp;gt; {
    setSelected(e.target.value);
  };

  const [count, setCount] = useState(0);
  const onCountChange = e =&amp;gt; {
    setCount(e.target.value);
  };

  const onOrder = () =&amp;gt; {
    setTimeout(() =&amp;gt; {
      alert(`You ordered ${count} ${selected}`);
    }, 3000);
  };

  useEffect(() =&amp;gt; {
    document.title = `Selected - ${selected}`;
  });

  useEffect(() =&amp;gt; {
    // eslint-disable-next-line
    console.log('logger', selected, count);
  });

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={onProductChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
        &amp;lt;input type="number" min={0} value={count} onChange={onCountChange} /&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;

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

&lt;/div&gt;



&lt;p&gt;Yes simple like that. &lt;code&gt;useEffect&lt;/code&gt; gets second argument. It's an array.&lt;br&gt;&lt;br&gt;
In that array you put any variable which change should trigger the effect.&lt;br&gt;&lt;br&gt;
In case those variables don't change. The effect will not run.  &lt;/p&gt;

&lt;p&gt;Just add another &lt;code&gt;console.log&lt;/code&gt; to that effect and you will see it now runs only when the &lt;code&gt;selected&lt;/code&gt; changes.&lt;/p&gt;
&lt;h3&gt;
  
  
  Re-usable hooks
&lt;/h3&gt;

&lt;p&gt;Let's dive right into coding.&lt;/p&gt;

&lt;p&gt;We create a new &lt;code&gt;hooker.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

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

// updateDocumentTitle name is bad the custom hook name should start with "use"
export const useDocumentTittle = title =&amp;gt; {
  useEffect(() =&amp;gt; {
    document.title = title;
  }, [title]);
};

export const useLogger = (...args) =&amp;gt; {
  useEffect(() =&amp;gt; {
    // eslint-disable-next-line
    console.log('logger', ...args);
  });
};

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

&lt;/div&gt;



&lt;p&gt;Then we do this in our &lt;code&gt;MenuFC.js&lt;/code&gt;&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';
import {useDocumentTittle, useLogger} from './hooker';

const MenuFc = () =&amp;gt; {
  const [selected, setSelected] = useState('Purple Haze');
  const onProductChange = e =&amp;gt; {
    setSelected(e.target.value);
  };

  const [count, setCount] = useState(0);
  const onCountChange = e =&amp;gt; {
    setCount(e.target.value);
  };

  const onOrder = () =&amp;gt; {
    setTimeout(() =&amp;gt; {
      alert(`You ordered ${count} ${selected}`);
    }, 3000);
  };

  useDocumentTittle(`Selected - ${selected}`);
  useLogger(selected, count);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Product: &amp;lt;/b&amp;gt;
        &amp;lt;select onChange={onProductChange}&amp;gt;
          &amp;lt;option value="Purple Haze"&amp;gt;Purple Haze&amp;lt;/option&amp;gt;
          &amp;lt;option value="Amnesia"&amp;gt;Amnesia&amp;lt;/option&amp;gt;
          &amp;lt;option value="GoGreen"&amp;gt;GoGreen&amp;lt;/option&amp;gt;
        &amp;lt;/select&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;b&amp;gt;Count: &amp;lt;/b&amp;gt;
        &amp;lt;input type="number" min={0} value={count} onChange={onCountChange} /&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={onOrder}&amp;gt;Order&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MenuFc;

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

&lt;/div&gt;



&lt;p&gt;That's right!&lt;br&gt;
Now we can use any of these hooks in any &lt;strong&gt;React Functional Component&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's how simple sharing logic can be.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some general rules for hooks
&lt;/h3&gt;

&lt;p&gt;Remember that custom hooks should be named as native hooks. The &lt;strong&gt;use&lt;/strong&gt; word should be &lt;strong&gt;used&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Hooks cannot be in conditions. But you can have conditions in your &lt;code&gt;useEffect&lt;/code&gt; callback&lt;/p&gt;

&lt;p&gt;And so on... You definitely should check &lt;a href="https://www.npmjs.com/package/eslint-plugin-react-hooks" rel="noopener noreferrer"&gt;eslint-plugin-react-hooks&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  To be continued...
&lt;/h3&gt;

&lt;p&gt;And that's it for hooks intro!&lt;br&gt;&lt;br&gt;
Soon I'll dive deeper into more specific cases...&lt;br&gt;&lt;br&gt;
So keep up with me!&lt;br&gt;
See you soon!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reacthooks</category>
      <category>demo</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
