|
| 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 | + |
| 13 | + |
| 14 | +Looking with `ick about` and ignoring documentation, the word *about* appears in these files: |
| 15 | + |
| 16 | + |
| 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 | + |
| 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 | + |
| 38 | + |
| 39 | +#### Add to `./routes.js` to connect the `/hello` url path to the component: |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +#### Add to `./src/containers/App/App.js` to add "Hello" to the NavBar |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +And voila, the new 'Hello' page: |
| 49 | + |
| 50 | + |
| 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 | + |
| 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. |
0 commit comments