lab3
lab3
Objectives:
Setup a React Project.
Put the UI Components in Place.
Render the created components and context in App.js.
View your app on the browser.
1. Go to the project repository on this link which has the partially developed code for react code.
2. Create a fork of the repository into your own. You will need to have a github account of your own to do so.
Copied!
6. This will clone the repository with Shopping application files in your home directory in the lab environment. Check the same with the
following command.
1. 1
1. ls
Copied! Executed!
1. cd kduia-shopping-app && ls
Copied! Executed!
8. All packages required to be installed are listed in package.json. Run npm install -s, to install and save those packages.
1. 1
1. npm install -s
Copied! Executed!
The image above displays the Shopping application page you will be developing in this sample project. This lab will help you in gaining the
knowledge about react to build a shopping app and complete the tasks provided in final project: Budget Allocation application.
CartValue
ExpenseItem
ExpenseList
ItemSelected
Location
All of these components will be using redux for state management through AppContext.js. You can open the AppContext.js by clicking on the
below button.
In AppContext.js you will be creating reducer, which is used to update the state, based on the action. Then you will set the initial state for
the Shopping Cart. You will be creating the Provider component which wraps the components you want to give access to the state.
1. Make changes to AppContext.js component. Open the code, in the src/context/AppContext.js. Copy the below code and paste in the
AppContext.js
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
15. 15
16. 16
17. 17
18. 18
19. 19
20. 20
21. 21
22. 22
23. 23
24. 24
25. 25
26. 26
27. 27
28. 28
29. 29
30. 30
31. 31
32. 32
33. 33
34. 34
35. 35
36. 36
37. 37
38. 38
39. 39
40. 40
41. 41
42. 42
43. 43
44. 44
45. 45
46. 46
47. 47
48. 48
49. 49
50. 50
51. 51
52. 52
53. 53
54. 54
55. 55
56. 56
57. 57
58. 58
59. 59
60. 60
61. 61
62. 62
63. 63
64. 64
65. 65
66. 66
67. 67
68. 68
69. 69
70. 70
71. 71
72. 72
73. 73
74. 74
75. 75
76. 76
77. 77
78. 78
79. 79
80. 80
81. 81
82. 82
83. 83
84. 84
85. 85
86. 86
87. 87
88. 88
89. 89
90. 90
91. 91
92. 92
93. 93
94. 94
95. 95
96. 96
97. 97
98. 98
99. 99
100. 100
Copied!
In AppContext.js you are setting the initial state of Expenses and Location. You can see how the items, their respective unit price are all
added to Expenses. Here, you are adding an initial expenses, creating a Provider component, setting up the useReducer hook which will
hold your state, and allow you to update the state via dispatch.
Adding a new case to the switch statement called “ADD_QUANTITY”, “RED_QUANTITY”, “DELETE_ITEM”, “CHG_LOCATION”.
2. Make changes to CartValue.js component. You can open the CartValue.js by clicking on the below button.
Copied!
Here, you are importing AppContext from your Context.Import the useContext hook, and pass your AppContext to it - this is how a
component connects to the context in order to get values from global state. The Bootstrap Alert classes are used to give a nice gray
background by adding some text and hard coding a value.
Now if you change the budget in AppContext and reload your browser, you will see the budget updates on the UI.
3. Make changes to ExpenseList.js component. You can open the ExpenseList.js by clicking on the below button.
Copied!
In ExpenseList you are importing your AppContext and useContext hook like before. Here, you are creating a list, using the map function
to iterate over the expenses, and displaying an ExpenseItem component.
4. Make changes to ExpenseItem.js component.You can open the ExpenseItem.js by clicking on the below button.
Copied!
In ExpenseItem you are importing dispatch from Context, which allows you to dispatch a delete action. You are creating a function that
gets called when the delete icon is clicked.
5. Make changes to ItemSelected.js component.You can open the ItemSelected.js by clicking on the below button.
Copied!
In ItemSelected, you are importing AppContext and useContext as usual and getting dispatch from your global state. You are creating an
event to reduce or add quantity.You are dispatching an action, with a type and your payload. The type tells the reducer how to update the
state.
6. Make changes to Location.js component. You can open the Location.js by clicking on the below button.
Copied!
In Location.js, you are importing AppContext and adding changeLication class to change the location along with its currencies. When you
change the Location, currencies will be updated at all the required places.
Copied!
Here, you are importing different components,adding a bootstrap container that helps you center your App on the page. Importing your
AppProvider and nested your components in the AppProvider element.
1. npm start
Copied!
2. Click on the Skills Network button on the left, it will open the “Skills Network Toolbox”. Then click the Other then Launch Application.
From there you should be able to enter the port 3000.
Author(s)
Pallavi Rai
Change Log