title: Using redux's useDispatch in a custom hook yields an error
body: <p>I'm trying to implement useDispatch in a custom hook that dispatches a redux action, but I'm getting the following error:</p>
  
  <pre><code>Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app
  </code></pre>
  
  <p><strong>CODE:</strong></p>
  
  <p>modules file</p>
  
  <pre><code>import { useDispatch, useSelector } from 'react-redux'
  
  export function useFetchEvents() {
    const dispatch = useDispatch()
    const { items, loading } = useSelector(state =&gt; state.events)
    if (items.length === 0) {
      dispatch(requestEvents(true))
    }
  }
  </code></pre>
  
  <p>functional component</p>
  
  <pre><code>import { useFetchEvents } from '../../../modules/events'
  
  const FrontPage = () =&gt; {
    return(
      &lt;div&gt;
        Front Page
        &lt;button onClick={useFetchEvents}&gt;
          Fetch events
        &lt;/button&gt;
      &lt;/div&gt;
    )
  }
  
  export default FrontPage
  </code></pre>
  
  <p>I've seen the error and read the rules regarding hooks, but if I understand it correctly I should be able to use useDispatch in a custom hook. Like in the following working examples:</p>
  
  <p><a href="https://github.com/mikeour/drinks_drinks_drinks/blob/master/src/hooks/index.js" rel="noreferrer">https://github.com/mikeour/drinks_drinks_drinks/blob/master/src/hooks/index.js</a></p>