title: How to set data from a react hook inside of a function or event?
body: <p>I'm trying to learn to create hooks so I can re-use data that I have to change in different components.</p>
  
  <p>I'm using <a href="https://material-ui.com/components/tabs/" rel="nofollow noreferrer">Material UI's Tabs</a> and need to use useTab, a custom hook to change the tab id.</p>
  
  <pre><code>import React, { useContext } from 'react';
  import { ProductsContext } from './ProductsContext';
  import AppBar from '@material-ui/core/AppBar';
  import Tabs from '@material-ui/core/Tabs';
  import Tab from '@material-ui/core/Tab';
  import { useTab } from '../../hooks/tab';
  
  const ProductsNav = () =&gt; {
      const {products, categories, loading} = useContext(ProductsContext);
      const [tabValue] = useTab(0);
  
      const handleTabChange = (e, newTabValue) =&gt; {
          useTab(newTabValue);
      }
  
      return (
          &lt;div className="products"&gt;
              &lt;AppBar position="static"&gt;
                  &lt;Tabs value={tabValue} onChange={ handleTabChange }&gt;
                      {
                          Array.from(categories).map(category =&gt; (
                              !category.unlisted &amp;&amp; (&lt;Tab label={category.title} key={category.id}/&gt;)
                          ))
                      }
                  &lt;/Tabs&gt;
              &lt;/AppBar&gt;
  
          &lt;/div&gt;
      ); 
  };
  
  export default ProductsNav;
  </code></pre>
  
  <p>I know it does this with child functions in the docs, but I'm trying to not just copy and paste and do it in my own way.</p>
  
  <p>Here is my custom useTab hook:</p>
  
  <pre><code>import {useState, useEffect} from 'react';
  
  export const useTab = (selectedTab) =&gt; {
      const [tabValue, setTabValue] = useState(0);
      useEffect(() =&gt; {
          setTabValue(selectedTab);
      }, []);
      return [tabValue];
  }
  </code></pre>
  
  <p>I'm of course getting an error I can't use a hook inside of a function, but I'm confused how else to do this. </p>
  
  <p>How can I change tabValue from useTabs? </p>