title: How to call useDispatch in a callback
body: <p>I got a React component which is trying to get some data, and is calling an <code>onSuccess(result)</code> call back upon a successful retrieval of data. </p>
  
  <p>I need to save the data to redux. I created custom hooks which are using <code>useDispatch</code>, and I'm trying to do something like this:</p>
  
  <pre><code>&lt;MyComponent onSuccess = {res =&gt; myCustomHook(res)} /&gt;
  </code></pre>
  
  <p>but I get an error because an hook can not be called inside a callback.</p>
  
  <p>I know that hooks can only be called at the top level of a functional component.. So how can I achieve what I need? </p>
  
  <p>The custom hook:</p>
  
  <pre><code>    export function useSaveData(type, response)
    {
      if(!type|| !response)
      {
        throw new Error("got wrong parameters for useSaveData");
      }
      let obj= {
        myData1: response.data1,
        myData2: response.data2
      };
      sessionStorage.setItem(type, JSON.stringify(obj));
      const dispatch = useDispatch();
      dispatch(actionCreator.addAction(type, obj));
    }
  </code></pre>