title: Can I dispatch vuex action inside getter function?
body: <p>I want to dispatch action inside getter function.
  <br>
  1. Is it possible and right.
  <br>
  2. If yes how can I do it?   </p>
  
  <p>I guess it will be something like this dispatch('GET_BOOKS');</p>
  
  <pre><code>const getters = {
      getAllBooksDispatch: (state, getters, dispatch) =&gt; {
          if (state.books === null) {
              dispatch('GET_BOOKS');
          }
          return state.books
      },
  };
  </code></pre>
  
  <p>But it does not work.
  <br>
  So my store file looks like this.  </p>
  
  <pre><code>const initialState = {
      books: null
  };
  
  const getters = {
      getAllBooksDispatch: (state, getters, dispatch) =&gt; {
          if (state.books === null) {
              dispatch('GET_BOOKS');
          }
          return state.books
      },
  };
  
  const mutations = {
      SET_BOOKS: (state,{data}) =&gt; {
          console.log('SET_BOOKS mutations')
          state.books = data;
      },
  };
  
  const actions = {
      GET_BOOKS: async ({ commit }) =&gt; {
          let token = users.getters.getToken;
  
          let query = new Promise((resolve, reject) =&gt; {
              axios.get(config.api + 'books', {token}).then(({data}) =&gt; {
                  if (data) {
                      commit('SET_BOOKS', {data: data})
                      resolve()
                  } else {
                      reject(data.message);
                  }
              }).catch(() =&gt; {
                  reject('Error sending request to server!');
              })
          })
  
      },
  };
  </code></pre>