title: How to make calculations using Redux toolkit?
body: <p>I've camed accross the question of calculations in redux using redux-toolkit. In my concrete example I'm trying to calculate a few things:</p>
  <ol>
  <li>The price of unit costs * unit;</li>
  <li>To add every multiplied price;</li>
  <li>After making Sub-total I want to calculate from it the % of tax(Total Vat);</li>
  <li>And the final stage is to calculate Sub-total + Total Vat.</li>
  </ol>
  <p>Screenshot here<a href="https://i.stack.imgur.com/l6SLB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l6SLB.jpg" alt="How to make such calculations?" /></a></p>
  <p>For example I've got 3 items:</p>
  <pre><code>        1) item_name: &quot;A box of happiness&quot;,
          unit_costs: &quot;10&quot;,
          unit: &quot;3&quot;,
          2) item_name: 'Unicorn Tears',
          unit_costs: '1'
          unit: '3',
          3) item_name: 'Rainbow Machine',
          unit_costs: '1',
          unit: '4',
  </code></pre>
  <p>So, the Sub-total would be 37(30+3+4). The % of tax is 10% and It'll score = 7. And the Grand Total will be 44. And the % of tax should be dynamic. If i enter 20% it should calculate according to this value.</p>
  <p>I'm novice in redux and redux-toolkit and want to know how to make such calculations. I would also appreciate if u show how to do in my concrete PET project - <a href="https://github.com/stepan-slyvka/test-project" rel="nofollow noreferrer">https://github.com/stepan-slyvka/test-project</a></p>
  <p>Should I do everything in <em><strong>invoice-slice.js</strong></em> file by creating new reducer? Or i can do calculations in <em><strong>AddInvoiceItem.js</strong></em>, <em><strong>EditInvoiceItem.js</strong></em> and in <em><strong>InvoiceItemDescription.js</strong></em> files? Thank you in advance :)</p>
  <p><em><strong>IMPORTANT</strong></em> - I want to make everything dynamic, so with each keystroke I wanna update the values.</p>
  <p><em><strong>UPDATE</strong></em></p>
  <p>I've discovered that I need to inititalize reducer for my calculations, but it shows error of</p>
  <blockquote>
  <p>Uncaught TypeError: Cannot read properties of undefined (reading 'forEach')</p>
  </blockquote>
  <p>When I try to do this code in my <em><strong>invoice-slice.js</strong></em> file:</p>
  <pre><code>calcPrice(state, action) {
        let total = 0;
        state.invoices.ITEMS.forEach((item) =&gt; {
          total = item.unit_costs * item.unit;
          total += item.total;
        });
        state.total = total;
      },
  </code></pre>
  <p>And as I know I should pick dynamic values with useSelector hook.</p>
  <p>Also want to do reducers to calculate Vat(%) and Grand-Total, but I can't, because I have to do correctly first one.</p>
  <p><a href="https://codesandbox.io/s/wizardly-darkness-8o2pcs?fontsize=14&amp;hidenavigation=1&amp;theme=dark" rel="nofollow noreferrer"><img src="https://codesandbox.io/static/img/play-codesandbox.svg" alt="Edit wizardly-darkness-8o2pcs" /></a></p>