Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. import shopping from './zadanie02';
  5.  
  6.  
  7. class Receipt extends React.Component {
  8. render() {
  9. const rows = this.props.shopping.map(item => {
  10. return (
  11. <tr>
  12. <td>{item.name}</td>
  13. <td>{item.count}</td>
  14. <td>{item.price}</td>
  15. <td>{(item.count * item.price).toFixed(2)}</td>
  16. </tr>
  17. )
  18. });
  19.  
  20. const sum = this.props.shopping.reduce((prev, curr) => {
  21. return prev + (curr.count * curr.price);
  22. }, 0);
  23.  
  24. return (
  25. <table>
  26. <thead>
  27. <tr>
  28. <th>nazwa</th>
  29. <th>cena</th>
  30. <th>ilość</th>
  31. <th>suma</th>
  32. </tr>
  33. </thead>
  34. <tbody>
  35. {rows}
  36. </tbody>
  37. <tfoot>
  38. <tr>
  39. <td>Razem</td>
  40. <td>-</td>
  41. <td>-</td>
  42. <td>{ sum.toFixed(2) }</td>
  43. </tr>
  44. </tfoot>
  45. </table>
  46. )
  47. }
  48. }
  49.  
  50. class App extends React.Component {
  51. render() {
  52. return <Receipt shopping={ shopping } />
  53. }
  54. }
  55.  
  56. document.addEventListener('DOMContentLoaded', function(){
  57. ReactDOM.render(
  58. <App/>,
  59. document.getElementById('app')
  60. );
  61. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement