Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. import React from 'react';
  2.  
  3. class App extends React.Component {
  4.  
  5. constructor() {
  6. super();
  7. this.state = {
  8. id: null,
  9. userId: 1,
  10. food: '',
  11. cost: '',
  12. status: false,
  13. foodItem: {},
  14. foodItems: [],
  15. editing: false
  16. };
  17.  
  18. this.handleInputChange = this.handleInputChange.bind(this);
  19. this.addFoodItem = this.addFoodItem.bind(this);
  20. }
  21.  
  22. handleInputChange(event) {
  23. event.preventDefault();
  24. const target = event.target;
  25. const value = target.value;
  26. const name = target.name;
  27.  
  28. this.setState({
  29. [name]:value
  30. })
  31. }
  32.  
  33. addFoodItem(event){
  34. event.preventDefault()
  35. if (!this.state.food) return;
  36. const foodItem = {
  37. id: this.state.foodItems.length + 1,
  38. food: this.state.food,
  39. cost: this.state.cost,
  40. userId: this.state.userId,
  41. statu: this.state.status
  42. };
  43.  
  44. this.setState({
  45. food: '',
  46. cost: '',
  47. foodItem: foodItem,
  48. foodItems: [...this.state.foodItems, foodItem]
  49. })
  50. }
  51. }
  52.  
  53. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement