Skip to content

Commit ec430d1

Browse files
committed
Adding ReactJS documentation
1 parent 4ec30e0 commit ec430d1

File tree

4 files changed

+155
-2
lines changed

4 files changed

+155
-2
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ Welcome to a custom Copilot workshop prepared by GitHub’s Expert Service team!
88
2. [Before getting started](docs/2_BeforeGettingStarted/README.md)
99
3. [Build a PostgreSQL database layer using Docker container](docs/3_BuildPostgreSQL/README.md)
1010
4. [Deploy the PostgreSQL package to GitHub Packages](docs/4_StoringPostgreSQLImageRegistry/README.md)
11-
5. [Build a Python Django server with REST](docs/5_BuildPythonDjango/README.md)
11+
5. [Build a Python Django server with REST](docs/5_BuildPythonDjango/README.md)
12+
6. [Build a front end layer with ReactJS](docs/6_BuildReactJS/README.md)
13+
7. [Lessons Learned - Best practices, gotchas, etc](docs/7_LessonsLearned/README.md)

docs/5_BuildPythonDjango/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
In the last stage, we built our PostgreSQL running inside a Docker container. In this time, we will try to build a simple Python Django server with REST API so it can retrieve and insert data.
66

7-
> In this example, you may want to start inside your FRONTEND-LAYER directory
7+
> In this example, you may want to start inside your BACKEND-LAYER directory
88
99
Try to open **Visual Studio Chat** and enter the following in your GitHub Copilot Chat.
1010

docs/6_BuildReactJS/README.md

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Let's build a front end with ReactJS
2+
3+
[Prev -Let's build a Python Django server with REST](../5_BuildPythonDjango/README.md) | [Next - Best practices and what we have learned](../7_LessonsLearned/README.md)
4+
5+
Last time, we created our backbone layers in Python Django server to retrieve the data from PostgreSQL database. In this time, we will create a ReactJS application that is going to communicate our Django server REST API. However, if we ask too many things first, we might not get the exact result that we want. Let’s start with a very simple step that can help us to get started with ReactJS projects from scratch and work our way from there.
6+
7+
> In this example, you may want to start inside your FRONTEND-LAYER directory
8+
9+
📝 **TIP**
10+
> Visual Studio Code Chat is still in beta, so things will improve much better later on. However, as of right now, the more complex thing you ask for the first time, it might have a hard time figuring out what you exactly want the first time. It is better to start with something simple, make sure that it works, and builds your way from there.
11+
12+
Let's fire up your GitHub Copilot chat and ask this question.
13+
14+
```bash
15+
Can you help to create a ReactJS application that runs on localhost 3000?
16+
```
17+
18+
![GitHub Copilot](./images/0_CopilotCreateReact.jpg)
19+
20+
This will create a set of instructions, but your result might slightly differ. First thing is to create a new project with `npx` command. You can select Run in Terminal option from GitHub Copilot Chat.
21+
22+
```bash
23+
npx create-react-app my-app
24+
```
25+
26+
You can also click GitHub Copilot Chat's command and select **Run in Terminal** option.
27+
28+
![Execute NPX Create](./images/1_RunNPX.jpg)
29+
30+
This will create a new ReactJS project from scratch. Feel free to change the project name `my-app` to something else.
31+
32+
![Created NPX](./images/2_CreatedReactJS.jpg)
33+
34+
Next step is to `cd` into the directory.
35+
36+
```bash
37+
cd my-app
38+
```
39+
40+
![CD into directory](./images/3_CDDirectory.jpg)
41+
42+
Next, you can start the project with the following command.
43+
44+
```bash
45+
npm start
46+
```
47+
48+
![NPM Start](./images/4_NPMStart.jpg)
49+
50+
This will start a local server in port `3000`, which is usually default for a front end server like ReactJS or NodeJS.
51+
52+
![Running ReactJS on localhost](./images/5_RunningLocalhost.jpg)
53+
54+
This will automatically open ReactJS application in your browser. But if it does not, you can visit `http://localhost:3000`. Your first ReactJS might look like this. Great!
55+
56+
![First ReactJS running](./images/6_FirstReact.jpg)
57+
58+
Try to open your project and explore the contents. From there, let’s try to open file `App.js` under `src` folder.
59+
60+
![React Contents](./images/7_ReactContents.jpg)
61+
62+
Your current `App.js` might look like this. Although GitHub Copilot can figure out the context from an opened directory file, it can understand better if you have open files. We will try to make this `App.js` to work with our Python Django REST API to retrieve our cat data.
63+
64+
![Initial App.js](./images/8_InitialApp.jpg)
65+
66+
Let's ask this following question in our GitHub Copilot Chat.
67+
68+
```bash
69+
Can you help me to modify my ReactJS script so it can retrieve cat's data from a REST API endpoint that is available through http://localhost:8000/cats?
70+
```
71+
72+
![Modify Question](./images/9_ModifyQuestion.jpg)
73+
74+
Highlight associated code lines in `App.js`. After that, you can select `App.js` and click **Insert at Cursor**. Try to save to automatically reload your ReactJS application. Visit `http://localhost:3000` again.
75+
76+
> WARNING: If you see an error like this, you need to configure **CORS** properly. Please follow the guideline earlier.
77+
78+
![CORS Error](./images/10_ErrorCORS.jpg)
79+
80+
However, you will most likely encounter an error like this.
81+
82+
![Error Fetching](./images/11_ErrorFetch.jpg)
83+
84+
This error occurs because GitHub Copilot really does not understand what response gets returned by the endpoint. This is another type of decision that you have to make as a developer to figure out what will be the best for your application.
85+
86+
If you revisit what gets returned by our API endpoint at `http://localhost:8000/cats`, this might look like this: there are lists inside a list, though yours might look different than mine.
87+
88+
![Sample Data](./images/12_SampleData.jpg)
89+
90+
It would probably be better to store JSON data inside the list. In generate, you might have to go back to your Django file and create models around it to better marshall and to unmarshall the data. But for now, let’s work with what we have.
91+
92+
In `App.js` file, I made following changes:
93+
- Under `useEffect()` function, chage `data` to `data.cats`
94+
- When it gets printed out, print `{cat}` instead of `{cat.name}`
95+
96+
![Fix File](./images/13_FixFile.jpg)
97+
98+
Then, when you save to reload your ReactJS web page, your updated ReactJS page might look like this.
99+
100+
![ReactJS showing table](./images/14_TableReact.jpg)
101+
102+
We can further parse those data to make them look prettier, but, for now, let’s focus on whether we can put those data into a nice table instead of a simple list. Select those HTML tags that print the data, and let’s ask this question in our GitHub Copilot Chat.
103+
104+
```bash
105+
How can I put those highlighted codes into a nice bootstrap table instead??
106+
```
107+
108+
![Print Table](./images/15_PrintTable.jpg)
109+
110+
So, just like any GitHub Copilot's suggestion, you might or might not get a suggestion related to what libraries to install. For me, I did not. I asked the following question.
111+
112+
```bash
113+
How can I install react-bootstrap?
114+
```
115+
116+
Then, GitHub Copilot Chat gave an instruction on how to install `react-bootstrap`.
117+
118+
```bash
119+
npm install react-bootstrap
120+
```
121+
122+
![Install React Bootstrap](./images/16_InstallReactBootstrap.jpg)
123+
124+
You can always choose **Run in Terminal** option if you get that a suggestion.
125+
126+
![Run as Terminal](./images/17_RunAsTerminal.jpg)
127+
128+
Then, I followed the rest of instructions to move into the table.
129+
130+
One last step that I wanted to make is to replace that ReactJS logo with my own cat logo. So, I asked this question in GitHub Copilot Chat.
131+
132+
```bash
133+
Can you help to replace that ReactJS logo with a cat logo?
134+
```
135+
136+
This printed out a series of instruction as following.
137+
138+
![Updating Logo](./images/18_UpdateLogo.jpg)
139+
140+
Then, when I reloaded my ReactJS after saving, I got something like this!
141+
142+
![Final Homepage](./images/19_FinalHome.jpg)
143+
144+
At this point, you might want to modify few other things like background color, update title, and adding buttons, etc. But as this workshop was mainly to use **GitHub Copilot** to build a full stack application, you should now have a good idea on how to proceed from this!
145+
146+
That is it for workshop. In next section, let's go over some what we have learned and some of best practices with GitHub Copilot.
147+
148+
[Prev -Let's build a Python Django server with REST](../5_BuildPythonDjango/README.md) | [Next - Best practices and what we have learned](../7_LessonsLearned/README.md)

docs/7_LessonsLearned/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Best practices and lessons learned from GitHub Copilot Workshop
2+
3+
Throughout this workshop, you have learned

0 commit comments

Comments
 (0)