title: vue and vuex: Evrything works, just want to see how shorten a code. Is there sort of watcher or what's the best way to shorten a code?
body: <p>Here are actions on how I implemented a cart. Don't really try to get what's going on the main point is a last line of code in each of a functions. Which is</p>
  <p><code>localStorage.setItem('cart', JSON.stringify(state.cart));</code></p>
  <p>those actions call mutations which in there turn changing variable <code>cart</code>, therefore after every change I save new cart in localStorage. Code works, but I don't like duplication. Is there better way doing ? I thought probably watcher on changing a state variable ?  But does anybody know if it exists ? And how to use it?</p>
  <pre><code>export async function removeItemFromCart({state, getters, commit}, id){
    let ind  = getters.index(id);
  
    commit('removeFromCart', ind);
    localStorage.setItem('cart', JSON.stringify(state.cart)); //here
  }
  
  export async function setCnt({state, getters, commit, rootGetters}, { id, cnt }){
    let ind  = getters.index(id);
  
    if(state.cart[ind].cnt + cnt &gt;= 1 &amp;&amp; state.cart[ind].cnt + cnt &lt;= rootGetters['watches/item'](id).cnt)
      commit('addCnt', { ind, cnt})
    localStorage.setItem('cart', JSON.stringify(state.cart)); //here
  }
  </code></pre>