Exercise 5.4B: Migrate Layout, Home, and Contact
Purpose:
To incorporate the Layout, Nav, and Footer into React, along with the simplest pages (Home and Contact),
then break the Nav into smaller components.
Step 1. Copy Nav Data to React Project
Copy your nav data from flaskapp/dat/nav.dat to reactapp/src/assets/nav.json.
When you use the .json file extension, import navLinks from './assets/nav.json' in React imports the JSON directly.
Step 2. Create Layout and Nav Components
In the reactapp/src/layouts directory, create:
- Nav.jsx: Nav component. Copy flaskapp/templates/common/nav.html, translate to JSX. Change all links from <a href> to <Link to> or <NavLink to> components. Import nav.json and replace for loops using tips below.
- Footer.jsx: Footer component. Copy flaskapp/templates/common/footer.html, translate to JSX.
- Layout.jsx: Layout components.
Copy relevant content of flaskapp/templates/common/layout.html, translate to JSX.
Mostly only Nav, Footer and wrapped content are relevant, as well as any elements you need to make your CSS work.
Determine which of the remaining content should be incorporated in reactapp/index.html, considering the tips below.
Use <Outlet> rather than {children} to deliver wrapped content to work with Route wrapping in App.jsx.
Use the tips below as needed for JSX translation.
Tips for Converting Jinja Templates to JSX Components
The following tips are general for converting HTML / Jinja content to JSX / React content:
-
Wrap HTML content in React functional component: function ComponentName() { return <CONTENT>; }
and add export default ComponentName;.
- Convert HTML to JSX:
- Close all tags. Use /> as an XML closer on childless tags like <input>, <img>, <br>, <hr>.
- Traditional HTML tags must be in lowercase, React Component tags in uppercase.
- All element properties must take quoted strings, React vars, integers or no value.
- Generally change hyphenated attributes to camelCase.
- Generally change event handler attributes to camelCase and set their value to functions in curly braces, not strings.
- Change class= attributes to className= attributes.
- Change style tags, if used, to use an object instead of a string.
- Components can return only one tag. If multiple are needed, use the empty tag <></>
- Replace <a href> tags with <Link> or <NavLink> components to avoid needless refreshes.
- Move <head>, <body>, <link>, <script> content out of components.
Everything is mounted in <div id=root>.
Use index.html to contain <title>, <meta>, fav icons, external style sheets, and external scripts.
Internal style sheets can either be incorporated in index.html or imported.
Javascript files should usually be imported into components instead of using <script> tags, except in index.html.
- The React Dev Server can help you find issues because it will give errors.
- There are now two types of images, front end and back end:
- For images in reactapp/public, you can set src either to "public/images/..." or "images/..." The second is recommended.
- For images in flaskapp/static, ok if src does not work until we connect the back end.
-
Replace Jinja blocks as follows:
|
{% extends %}
|
Remove. Use Layout component at the App.jsx level instead.
|
|
{% block main %} ... {% endblock %}
|
Remove. Use Layout component at the App.jsx level instead.
|
|
{% include %}
|
Replace with component tags, remembering first to import the relevant component.
|
|
{% if condition %} ... {% else %} ... {% endif %}
|
Replace with { condition ? ... : ... } (conditional rendering)
|
|
{% for var in iterable %} ... {% endfor %}
|
Replace with { iterable.map(var => { ... }) }
|
|
{{ var }}
|
Replace with { var }. Make sure var contents are accessible. If needed, use dummy variables for a monkey patch temporarily.
When variables or expressions are inside of quotes in the HTML
(Example: <input value="professional{{ id }}.{{ ext }}">),
remove the outer quotes and make a string expression in curly braces
(Example: <input value={"professional" + id + "." + ext}>)
|
Step 3. Migrate Home and Contact Routes
In reactapp/src/pages, create page components for Home and Contact.
Copy the HTML from your flaskapp/templates directory (home.html and contact.html).
Translate to JSX where needed.
In App.jsx, connect the appropriate routes to <Home /> and <Contact /> inside the <Route> tags.
Step 4. Wrap the Layout
As you finish migrating each route, wrap the finished route in <Route element=<Layout /> >.
Note: If you chose to use FlaskPatch from Exercise 5.1, leave the FlaskPatch routes outside of the Layout until you are ready to replace them with the React content, to avoid a double nav and footer. If you did not use FlaskPatch, this is not a concern.
Step 5. Refactor Nav with HamburgerButton and NavLinks Components
To make your Nav more simple and high-level, break down Nav into components. At a minimum:
- In the widgets directory, a HamburgerButton component (reactapp/src/widgets/HamburgerButton.jsx)
- In the containers directory, a NavLinks component (reactapp/src/widgets/NavLinks.jsx) that does the .map function.
- If more components help make Nav simple and high-level, make and use them.
Since the id in the collapsible div needs to match the "data-bs-target" and "aria-control" attributes,
use props to ensure they match in the HamburgerButton and NavLinks components.
Note on Organizing React Code
Take a moment to reflect on how the above instructions above follow the following organizational principles for React code:
-
NavLinks is a "smart component" because it uses a loop.
It should go in the containers subdirectory.
-
HamburgerButton is a "dumb component," mostly presentational, using only simple variable substitutions.
It should go in the widgets subdirectory.
-
Nav is a "layout component" (layouts subdirectory).
These should be kept simple and high-level.
We use HamburgerButton and NavLinks to hide messy detail.
-
Home and Contact are "page components" (pages subdirectory).
These should also be kept simple and high-level like layout components.