Skip to content

Commit 3fdcf22

Browse files
committed
Merge pull request #883 from merriam/master
docs(guide): Add 'Adding A Page' guide
2 parents 318027e + f3cab9b commit 3fdcf22

File tree

11 files changed

+94
-0
lines changed

11 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ A demonstration of this app can be seen [running on heroku](https://react-redux.
8181
* [Exploring the Demo App](docs/ExploringTheDemoApp/ExploringTheDemoApp.md) is a guide that can be used before you install the kit.
8282
* [Installing the Kit](docs/InstallingTheKit/InstallingTheKit.md) guides you through installation and running the development server locally.
8383
* [Adding Text to the Home Page](docs/AddingToHomePage/AddingToHomePage.md) guides you through adding "Hello, World!" to the home page.
84+
* [Adding A Page](docs/AddingAPage/AddingAPage.md) guides you through adding a new page.
8485
* [React Tutorial - Converting Reflux to Redux](http://engineering.wework.com/process/2015/10/01/react-reflux-to-redux/), by Matt Star
8586
If you are the kind of person that learns best by following along a tutorial, I can recommend Matt Star's overview and examples.
8687

docs/AddingAPage/AddingAPage.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Adding A Hello Page
2+
3+
This guide adds a `/hello` page to the sample application by
4+
following the existing outline.
5+
6+
## Using Ack on About
7+
8+
Searching strings is one way to [grok](https://en.wikipedia.org/wiki/Grok) the structure
9+
of the kit and sample application. You can use *grep* or [ack](http://beyondgrep.com) (`brew install ack`).
10+
I use *ack* with this alias:
11+
12+
![ick Alias](ick_alias.png)
13+
14+
Looking with `ick about` and ignoring documentation, the word *about* appears in these files:
15+
16+
![ick for About](ick_about.png)
17+
18+
## Add the Hello page container
19+
20+
A new page requires new page renderer. Copy the About page to a
21+
new directory and trim out almost all of it:
22+
23+
* `cd ./src/containers && mkdir ./src/Hello` because each container goes in its own
24+
directory by convention.
25+
* `cp About/About.js Hello/Hello.js`
26+
27+
Edit `Hello/Hello.js` into this file:
28+
29+
![New Hello.js](new_hello.png)
30+
31+
32+
33+
## Edit three files to add Hello
34+
35+
#### Add to `./src/containers/index.js` to include and export the React component:
36+
37+
![Edit index.js](edit_index.png)
38+
39+
#### Add to `./routes.js` to connect the `/hello` url path to the component:
40+
41+
![Edit routes.js 1](edit_route1.png)
42+
![Edit routes.js 2](edit_route2.png)
43+
44+
#### Add to `./src/containers/App/App.js` to add "Hello" to the NavBar
45+
46+
![Edit App.js](edit_app.png)
47+
48+
And voila, the new 'Hello' page:
49+
50+
![Show Hello](show_hello.png)
51+
52+
# Take-away: Notice the trade-offs
53+
54+
The task of adding a new page exemplifies two trade-offs in the kit:
55+
**code versus convention** and the **cut and paste** style.
56+
57+
Convention is a set of constraining rules that automatically trigger
58+
routine configuration tasks. For example, WebPack automatically picked up the
59+
new directory `./src/containers/Hello` without adding to any configuration files.
60+
61+
On the other hand, routine code was added to `./src/containers/index.js` and
62+
`./src/routes.js` to handle the new page. A convention could automatically
63+
accomplish the same tasks at either compile or run time. The cost is new
64+
constraints, such as requiring `Hello/Hello.js` to be renamed
65+
`HelloPage/HelloPage.js`.
66+
67+
Following a style in the code that has no automatic effects is just organic
68+
growth, not convention. For example, developers reading `./src/containers/index.js`
69+
must stop and figure out why all subdirectories except `DevTools` are exported.
70+
(`DevTools`)[`./src/containers/DevTools/DevTools.js`](https://github.com/erikras/react-redux-universal-hot-example/blob/master/src/containers/DevTools/DevTools.js)
71+
contains a single function which should be
72+
[randomly](https://github.com/erikras/react-redux-universal-hot-example/issues/808)
73+
moved to `./src/utils` or `./src/helpers`. Using a convention rule that all
74+
containers must contain an exported React component would raise an error.
75+
Organic growth leads to disorder in a project.
76+
77+
Similarly, the **cut and paste** style of coding also degrades the project.
78+
For example, In `App.js`, the new *NavItem* tag included a new value for the
79+
*eventkey* property. The *eventkey* property is
80+
[poorly](https://github.com/react-bootstrap/react-bootstrap/issues/320)
81+
[understood](https://github.com/react-bootstrap/react-bootstrap/issues/432).
82+
All *eventkey* fields in `App.js` are unused and can be removed. The
83+
**cut and paste** style just compounds an
84+
[old error](https://github.com/erikras/react-redux-universal-hot-example/commit/d67a79c1e7da5367dc8922019ca726e69d56bf0e)
85+
and reinforces confusion.
86+
87+
![Edit App revisted](edit_app2.png)
88+
89+
The use of the **cut and paste** style raises well known issues in
90+
maintenance, documentation, and code quality. It is not for use in
91+
production code.
92+
93+
Some choices about trade-offs are easier than others.

docs/AddingAPage/edit_app.png

53.7 KB
Loading

docs/AddingAPage/edit_app2.png

53.4 KB
Loading

docs/AddingAPage/edit_index.png

41.3 KB
Loading

docs/AddingAPage/edit_route1.png

49.5 KB
Loading

docs/AddingAPage/edit_route2.png

48.5 KB
Loading

docs/AddingAPage/ick_about.png

178 KB
Loading

docs/AddingAPage/ick_alias.png

18.1 KB
Loading

docs/AddingAPage/new_hello.png

94.4 KB
Loading

0 commit comments

Comments
 (0)