You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: P05-Adding-React-Router/content.md
+6-6
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,9 @@ To work with React Router it helps to understand some terminology.
20
20
-**Router** - A parent component that manages Routes
21
21
-**Route** - A component that displays another component
22
22
23
-
Think of the Router as the manager, you only need one of these. The Router checks the URL in the address bar and passes this information to its descendant Routes.
23
+
Think of the Router as the manager, you only need one of these. The Router checks the URL in the address bar and passes this information to its descendant Routes.
24
24
25
-
Imagine a website with three pages built with React using React Router. The Router and Route components might be arranged like this:
25
+
Imagine a website with three pages built with React using React Router. The Router and Route components might be arranged like this:
26
26
27
27
- Router
28
28
- Route - Home Page
@@ -31,7 +31,7 @@ Imagine a website with three pages built with React using React Router. The Rout
31
31
32
32
A Route is responsible for displaying components. Routes have a path property: when the path matches the URL in the address, the Route displays the appropriate component, otherwise not.
33
33
34
-
In code this might look like:
34
+
In code this might look like:
35
35
36
36
```js
37
37
<Router>
@@ -85,7 +85,7 @@ Router manages routes. It should be a top level component. For this reason you'l
Why `HashRouter as Router`? This is an alias. You're importing HashRouter but using it under the name `Router` instead. This will make it easier to make changes in the future if needed.
88
+
Why `HashRouter as Router`? This is an alias. You're importing HashRouter but using it under the name `Router` instead. This will make it easier to make changes in the future if needed.
Copy file name to clipboardexpand all lines: P06-Organizing-Files/content.md
+20-17
Original file line number
Diff line number
Diff line change
@@ -23,23 +23,26 @@ Organizing components and their dependencies in folders is one way to keep compo
23
23
>
24
24
> Move all of your components into this folder. It should include all of the following files:
25
25
>
26
-
> - src
27
-
> - components
28
-
> - About.js
29
-
> - App.css
30
-
> - App.js
31
-
> - App.test.js
32
-
> - index.css
33
-
> - index.js
34
-
> - POPOSDetails.js
35
-
> - POPOSList.css
36
-
> - POPOSList.js
37
-
> - POPOSSpace.css
38
-
> - POPOSSpace.js
39
-
> - serviceWorker.js
40
-
> - setupTests.js
41
-
> - Title.css
42
-
> - Title.js
26
+
```
27
+
- src
28
+
- App.css
29
+
- App.js
30
+
- App.test.js
31
+
- components
32
+
- About.js
33
+
- POPOSDetails.js
34
+
- POPOSList.css
35
+
- POPOSList.js
36
+
- POPOSSpace.css
37
+
- POPOSSpace.js
38
+
- serviceWorker.js
39
+
- setupTests.js
40
+
- Title.css
41
+
- Title.js
42
+
- index.css
43
+
- index.js
44
+
```
45
+
>
43
46
44
47
All of your components should now be in the `components` folder, **except for index.js and index.css**. After this change, your folder structure should look like the following:
At this point the List page will show the search results. Filter is returning a new array with the spaces that match your search results **the index of these new items won't match the position of those items in the data array.**
// Note the indexes - Notice 50 market is removed and 101 California is now index 1!
196
+
// 0:'100 Pine', 1:'101 California'
197
+
```
198
+
199
+
The code in our detail page relies on the index to find the information in the source data.
200
+
201
+
In the example code above if we use the index from the filtered data the details page would show 50 market instead of 101 California.
202
+
203
+
To fix this let's add an id property to our data.
204
+
205
+
> [info]
206
+
>
207
+
> Add a new file: `sfpopos-data.js` put this in the same folder as `sfpopos-data.json`.
208
+
>
209
+
> Add the following code to `sfpopos-data.js`:
210
+
>
211
+
```JS
212
+
importdatafrom'./sfpopos-data.json'
213
+
214
+
data.forEach((obj, i) => {
215
+
obj.id= i
216
+
})
217
+
218
+
exportdefaultdata
219
+
```
220
+
>
221
+
222
+
What happened here? This file imports `./sfpopos-data.json`, loops over the data and adds a new id property to each object. Where the objects originally looked like this:
223
+
224
+
```JS
225
+
{
226
+
title:'Transamerica Redwood Park',
227
+
desc:'Located in the shadow of the Transamerica Pyramid ...',
228
+
address:'600 Montgomery St San ...',
229
+
...
230
+
}
231
+
```
232
+
233
+
The objects now all look like this:
234
+
235
+
```js
236
+
{
237
+
id:0, // NEW id added here matches the index of this element!
238
+
title:'Transamerica Redwood Park',
239
+
desc:'Located in the shadow of the Transamerica Pyramid ...',
240
+
address:'600 Montgomery St San ...',
241
+
...
242
+
}
243
+
```
244
+
245
+
The last line exports the data. Our other files can now import the data from this file. You'll need to edit any of the files that import `./sfpopos-data.json` and change the import to `./sfpopos-data.js` (notice the difference `.json` to `.js`.)
246
+
247
+
> [info]
248
+
>
249
+
> Edit the following follows changing:
250
+
```JS
251
+
// Change
252
+
importdatafrom'../../sfpopos-data.json'
253
+
// to
254
+
importdatafrom'../../sfpopos-data.js'
255
+
```
256
+
>
257
+
> `components/RandomSpace.js`
258
+
> `POPOSDetails.js`
259
+
> `POPOSList.js`
260
+
>
261
+
262
+
> [info]
263
+
>
264
+
> Use the id from data rather than the index from map as the id for each space.
Here you replaced the index of the element in the array with the id of that element. Where the index might change when the array is filtered each object will always use the same value for the id.
290
+
183
291
### Style the search field
184
292
185
293
Here are a few ideas to style the search field. What you do here depends on where you positioned the search field. In my example code it ended up in the first grid cell in the list and I decided to go with it.
Sharing your work with world is a good thing. GitHub provides an easy way to publish your work. GitHub doesn't allow you to run backend code, upload files, or host a database. Within those limitations GitHub Pages provides a service for the reasonable cost of $0.
9
+
10
+
GitHub Pages allows you to publish static web sites with little more than the push of a button. The performance is also very good. The pages load quickly and are always available.
11
+
12
+
### Publishing react
13
+
14
+
React projects take a little more to publish. The React project source code doesn't run in the browser it must compiled/transpiled first. This process happen when you run your react code in the terminal with `yarn start` or `npm start`. If you want to publish your work you'll run `npm run build`. This will create a JS bundle and put it your public directly. This is the code that you would share with the world.
15
+
16
+
We can take this process one step further. Besides building the project we can build to a branch and publish that branch. This way your developer code that's in process can be worked on on the master branch while code that's been published can be served from another branch.
17
+
18
+
Luckily someone has made a library that automates the process.
0 commit comments