lab2
lab2
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.
4. Open a terminal window by using the menu in the editor: Terminal > New Terminal.
5. Clone the repository by running the command given below:
1. 1
Copied!
6. This will clone the repository with budget allocation app files in your home directory in the lab environment. Check the same with
the following command.
1. 1
1. ls
Copied! Executed!
1. cd ejtos-react_budget_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!
Budget
ExpenseItem
ExpenseList
ExpenseTotal (Spent so far)
Remaining
AllocationForm
All of these components will be using redux for state management through AppContext.js. You can verify the content of 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 departments. You will be creating the Provider component which wraps the components you want to give access to
the state.
Here, you are adding an initial budget, 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_EXPENSE”, “RED_EXPENSE”, “DELETE_EXPENSE”.
In Budget.js you will be adding text and value for your budget. You will be importing app context and 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.
In Remaining.js you will be importing expense and budget from context and getting the remaining value using subtraction.
In ExpenseTotal.js you will be adding useContext and AppContext. Taking the expenses from state.
In ExpenseList.js you will be using the map function to display the Expenseitem component.
In Expenseitem.js you will be importing dispatch from Context, which allows you to dispatch a delete action, creating a function
that gets called when the delete icon is clicked.
In AllocationForm.js you will be creating an expense object, containing the name and the cost. This is what will get dispatched as
the payload, and what you’ll use to update the state.
At start you will be creating a Budget, Remaining and Spent so far button.
1. Make changes to Budget.js component. Open the code, in the src/components/Budget.js. Copy the below code and paste in the
Budget.js
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
Copied!
Here, you are using the Bootstrap Alert classes to give a nice gray background by adding some text and hard coding a value.
2. Make changes to App.js. Open the code, in the src/App.js. Copy the below code and paste in the App.js in the space provided. You will
notice that Budget.js has been already imported for you in App.js.
1. 1
2. 2
3. 3
4. 4
1. // Budget component
2. <div className='col-sm'>
3. <Budget />
4. </div>
Copied!
3. Make sure you are in the ejtos-react_budget_app directory and run the server using the following command.
1. 1
1. npm start
Copied! Executed!
4. Click on the button below to launch the application or click on Skills Network button on the left, it will open the “Skills Network
Toolbox”. Then click Other then Launch Application. From there you should be able to enter the port 3000.
Budget Application
6. Make changes to Remaining.js component. Open the code, in the src/components/Remaining.js. Copy the below code and paste in the
Remaining.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
Copied!
Here, you are using the reduce function to get a total of all the costs, assigning this to a variable and displaying the variable in your JSX.
Now whenever the user adds an expense, this causes the state to update, which will cause all components connected to the context to
re-render and update themselves with new values.
7. Make changes to App.js. Open the code, in the src/App.js. Copy the below code and paste in the App.js in the space provided. Import
the component in the appropriate place using import Remaining from './components/Remaining';
1. 1
2. 2
3. 3
4. 4
1. //Remaining component
2. <div className='col-sm'>
3. <Remaining />
4. </div>
Copied!
8. Refresh the React app page launched in step 4 and verify the output.
9. Make changes to ExpenseTotal.js component. Open the code, in the src/components/ExpenseTotal.js. Copy the below code and paste
in the ExpenseTotal.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
Copied!
Here, you are using the reduce function to get a total of all the costs, assigning this to a variable and displaying the variable in your JSX.
Now whenever the user adds an expense, this causes the state to update, which will cause all components connected to the context to
re-render and update themselves with new values.
10. Make changes to App.js. Open the code, in the src/App.js. Copy the below code and paste in the App.js in the space provided.Import
the component in the appropriate place using import ExpenseTotal from './components/ExpenseTotal';
1. 1
2. 2
3. 3
4. 4
1. //ExpenseTotal component
2. <div className='col-sm'>
3. <ExpenseTotal />
4. </div>
Copied!
11. Refresh the React app page launched in step 4 and verify the output.
12. Make changes to ExpenseList.js component. Open the code, in the src/components/ExpenseList.js. Copy the below code and paste in
the ExpenseList.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
Copied!
Here, you are creating a list, using the map function to iterate over the expenses, and displaying an ExpenseItem component.
13. Make changes to ExpenseItem.js component. Open the code, in the src/components/ExpenseItem.js. Copy the below code and paste
in the ExpenseItem.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
Copied!
Here, you are dispatching an action. Your action contains the type (so the reducer knows how to update the state) and the payload. In
this case you are passing the ID of this expense (which you get from props when you rendered the ExpenseList).
14. Make changes to AllocationForm.js component. Open the code, in the src/components/AllocationForm.js. Copy the below code and
paste in the AllocationForm.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
Copied!
Here, you are adding form tags, adding a label/input for name, cost and action field, and adding values for various departments.
Copied!
Here, you are importing different components,adding a bootstrap container that helps you center your App on the page
Adding a title
Adding a Bootstrap row
Adding a column within the row for each of your components so far
Imported and Rendered the AllocationForm
Imported AppProvider and Nested components in the AppProvider element.
2. Refresh the React app page launched and verify the output.
Commit and push your local code to your remote git repository
This git pushed code will be cloned and used in final project.
Please refer to this lab Working with git in the Theia lab environment
Congratulations! You have completed this practice lab and know how to create components of a budget allocation application.
Author(s)
Pallavi Rai
Change Log