title: React hooks function component prevent re-render on state update
body: <p>I'm learning about React Hooks which means I'm going to have to move away from classes to function components. Previously in classes I could have class variables independent of the state that I could update without the component re-rendering. Now that I am attempting to re-create a component as a function component with hooks I have ran into the problem that I can't (as far as I know) make variables for that function so the only way to store data is through the <code>useState</code> hook. However this means my component will re-render whenever that state is updated.</p>
  
  <p>I've illustrated it in the example below where I attempted to re-create a class component as a function component that uses hooks. I want to animate a div if someone clicks on it, but prevent the animation from being called again if the user clicks while it's already animating.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class ClassExample extends React.Component {
    _isAnimating = false;
    _blockRef = null;
    
    onBlockRef = (ref) =&gt; {
      if (ref) {
        this._blockRef = ref;
      }
    }
    
    // Animate the block.
    onClick = () =&gt; {
      if (this._isAnimating) {
        return;
      }
  
      this._isAnimating = true;
      Velocity(this._blockRef, {
        translateX: 500,
        complete: () =&gt; {
          Velocity(this._blockRef, {
            translateX: 0,
            complete: () =&gt; {
              this._isAnimating = false;
            }
          },
          {
            duration: 1000
          });
        }
      },
      {
        duration: 1000
      });
    };
    
    render() {
      console.log("Rendering ClassExample");
      return(
        &lt;div&gt;
          &lt;div id='block' onClick={this.onClick} ref={this.onBlockRef} style={{ width: '100px', height: '10px', backgroundColor: 'pink'}}&gt;{}&lt;/div&gt;
        &lt;/div&gt;
      );
    }
  }
  
  const FunctionExample = (props) =&gt; {
    console.log("Rendering FunctionExample");
    
    const [ isAnimating, setIsAnimating ] = React.useState(false);
    const blockRef = React.useRef(null);
    
    // Animate the block.
    const onClick = React.useCallback(() =&gt; {
      if (isAnimating) {
        return;
      }
  
      setIsAnimating(true);
      Velocity(blockRef.current, {
        translateX: 500,
        complete: () =&gt; {
          Velocity(blockRef.current, {
            translateX: 0,
            complete: () =&gt; {
              setIsAnimating(false);
            }
          },
          {
            duration: 1000
          });
        }
      },
      {
        duration: 1000
      });
    });
    
    return(
      &lt;div&gt;
        &lt;div id='block' onClick={onClick} ref={blockRef} style={{ width: '100px', height: '10px', backgroundColor: 'red'}}&gt;{}&lt;/div&gt;
      &lt;/div&gt;
    );
  };
  
  ReactDOM.render(&lt;div&gt;&lt;ClassExample/&gt;&lt;FunctionExample/&gt;&lt;/div&gt;, document.getElementById('root'));</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.2/velocity.min.js"&gt;&lt;/script&gt;
  
  &lt;script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"&gt;&lt;/script&gt;
  
  &lt;div id='root' style='width: 100%; height: 100%'&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>If you click on the ClassExample bar(pink) you will see that it does not re-render while animating, however if you click on the FunctionExample bar(red) it will rerender twice while it is animating. This is because I'm using <code>setIsAnimating</code> which causes the re-render. I know it's probably not very performance-winning but I would like to prevent it if it's at all possible with a function component. Any suggestions/am I doing something wrong?</p>
  
  <p><strong>Update (attempted fix, no solution yet):</strong>
  Below user lecstor suggested possibly changing the result of useState to let instead of const and then setting it directly <code>let [isAnimating] = React.useState(false);</code>. This unfortunately does not work either as you can see in the snippet below. Clicking on the red bar will start its animation, clicking on the orange square will make the component re-render, and if you click on the red bar again it will print that <code>isAnimating</code> is reset to false even though the bar is still animating.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="true">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const FunctionExample = () =&gt; {
    console.log("Rendering FunctionExample");
  
    // let isAnimating = false; // no good if component rerenders during animation
    
    // abuse useState var instead?
    let [isAnimating] = React.useState(false);
    
    // Var to force a re-render.
    const [ forceCount, forceUpdate ] = React.useState(0);
  
    const blockRef = React.useRef(null);
  
    // Animate the block.
    const onClick = React.useCallback(() =&gt; {
      console.log("Is animating: ", isAnimating);
      if (isAnimating) {
        return;
      }
      
      isAnimating = true;
      Velocity(blockRef.current, {
        translateX: 500,
        complete: () =&gt; {
          Velocity(blockRef.current, {
            translateX: 0,
            complete: () =&gt; {
              isAnimating = false;
            }
          }, {
            duration: 5000
          });
        }
      }, {
        duration: 5000
      });
    });
  
    return (
    &lt;div&gt;
      &lt;div
        id = 'block'
        onClick = {onClick}
        ref = {blockRef}
        style = {
          {
            width: '100px',
            height: '10px',
            backgroundColor: 'red'
          }
        }
        &gt;
        {}
      &lt;/div&gt;
      &lt;div onClick={() =&gt; forceUpdate(forceCount + 1)} 
        style = {
          {
            width: '100px',
            height: '100px',
            marginTop: '12px',
            backgroundColor: 'orange'
          }
        }/&gt;
    &lt;/div&gt;
    );
  };
  
  ReactDOM.render( &lt; div &gt; &lt; FunctionExample / &gt; &lt; /div&gt;, document.getElementById('root'));</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.2/velocity.min.js"&gt;&lt;/script&gt;
  
  &lt;script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"&gt;&lt;/script&gt;
  
  &lt;div id='root' style='width: 100%; height: 100%'&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Update 2(solution):</strong>
  If you want to have a variable in a function component but not have it re-render the component when it's updated, you can use <code>useRef</code> instead of <code>useState</code>. <code>useRef</code> can be used for more than just dom elements and is actually suggested to be used for instance variables.
  See: <a href="https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables" rel="noreferrer">https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables</a></p>