title: Update state if response is ok
body: <p>So I have kind of a different problem here. My react app has a reducer that updates the state of a cart when a product is added. As i works right now i send this updated state to an API endpoint so save it to the database.</p>
  <pre><code>  const addToCart = (item, quantity = 1) =&gt; {
      dispatch({
        type: 'ADD_ITEM',
        payload: { item: item, quantity: quantity },
      });
    };
  </code></pre>
  <p>My api endpoint is called by a useEffect on this function.</p>
  <pre><code>  useEffect(() =&gt; {
      updateCart(state);
    }, [removeFromCart, addToCart]);
  </code></pre>
  <p>So basically what I am doing is that I send the state to the db for removing and adding items to cart. But this comes with a problem. If a user adds many items to cart very rapidly the state and what is saved inn the database is out of sync.</p>
  <p>Then I was thinking to revert the state if the response is not ok from the API endpoint. But a little unsure on how to do this. Anybody have any tips on how to solve this problem?</p>