title: How can i dispatch an action from a helper function in react native
body: <p>I have a <code>helper.js</code> file which contains all the helper function including the HTTP request handler with axios. Here are my <code>helper.js</code> file codes: </p>
  
  <pre><code>const HTTPRequest = (path, body, method = 'POST', authorizedToken = null) =&gt; {
      return new Promise((resolve, reject) =&gt; {
          let headers = {
              'Content-type': 'multipart/form-data',
          };
  
          // Set authorization token
          if (authorizedToken) {
              headers['Authorization'] = "JWT " + authorizedToken;  // Here i want to get user token from the redux store not though passing the token
  
          }
  
          const fulHeaderBody = {
              method,
              headers,
              timeout: 20,
              url: path
          };
  
  
  axios(fulHeaderBody).then(response =&gt; {
              if (response.success) {
                  resolve(response);
              } else {
  
                  // Show toast about the error
                  Toast.show({
                      text: response.message ? response.message : 'Something went wrong.',
                      buttonText: 'Okay',
                      type: 'danger'
                  });
                  resolve(response);
              }
          }).catch(error =&gt; {
              if (error.response) {
  
                 if(error.response.status === 401){
                      // I WANT TO DISPATCH AN ACTION HERE TO LOGOUT CLEAR ALL STORAGE DATA IN REDUX AND CHANGE THE STATE TO INITIAL
                 }else{
                      console.log('Error', error.message);
                 }
              }  else {
                  // Something happened in setting up the request that triggered an Error
                  console.log('Error', error.message);
              }
              console.log(error.config);
              reject(error);
          })
      });
  };
  export default HTTPRequest;
  </code></pre>
  
  <p>Now then, the problem I am facing is that, how can I dispatch an action from this helper function or how can I get <code>user token</code> from the redux store.</p>
  
  <p>I have tried creating an action name in <code>action.js</code> like this</p>
  
  <pre><code>export function helloWorld() {
      console.log('Hello world has been dispatched');
      return function(dispatch){
          dispatch(helloWorldDispatch());
      }
  }
  function helloWorldDispatch() {
      return {
          type: 'HELLO_WORLD'
      }
  }
  </code></pre>
  
  <p>and in the reducer :</p>
  
  <pre><code>switch (action.type) {
      case 'HELLO_WORLD': {
          console.log('Hello world Current state',state);
          return state
      }
      default:
          return state
  }
  </code></pre>
  
  <p>And calling it from <code>helper.js</code> like this</p>
  
  <p>inside <code>HTTPRequest</code> function </p>
  
  <pre><code>helloWorld();
  </code></pre>
  
  <p>I can see only the log <code>Hello world has been dispatched</code>, but I don not see any log from the dispatcher. </p>
  
  <p>Can anyone please tell how can I handle this. ?</p>