Purpose: To begin migrating your project from a Flask-only project into a React + Flask project.
Converting from Flask-only to React is more involved than converting from static to Flask, because everything must go into functions and because JSX syntax is stricter than HTML. For this reason, we will not attempt to move content over without changing file content as we did when transitioning from static to Flask. Instead, we will set up the directory structure, keep a functioning Flask app, replace and clean up the routes one at a time.
Set up your React+Flask directory structure as follows:
Make flaskapp subdirectory. Create a subdirectory called flaskapp in your project subdirectory, and move all your Flask project files into it. We will keep a functioning Flask app and clean up routes as we migrate them to React. You may want to keep non-project files, such as git files and shell scripts, in the main directory.
Set CORS policy in Flask. Modify flaskapp/app.py to import flask_cors and run CORS(app) to set the CORS policy as taught in Exercise 5.2E.
Make reactapp subdirectory. Create reactapp by following the instructions in Exercise 5.2A "Start a New React Project", including the cleanup instructions and the instructions to make it able to run in a subdirectory.
Make Component Subdirectories. Create the following required subdirectories to match project directory structure requirements:
CSS: Replace reactapp/src/index.css with the css from your flask app, except replace the "body" tag with "#root" so that everything applies to the <div id="root"> in React.
JS: In Project Part 4.5, you made javascript helper functions in index.js to handle AJAX requests and responses. Now, copy all such helper functions into reactapp/src/assets/api.js.
Images: You will now start needing to think about which images should be on the front end and which should be on the back end. The general rule is that static content (images that stay the same) should stay on the front end, but dynamic, data-driven content (in this case, your professionals and categories images, since the user might upload new ones) should stay on the back end. Copy images over to the public directory, except for the images of professionals and categories, which will stay in your Flask app.
Step 3.1: Set Up. Install React Router and set up main.jsx as instructed in Exercise 5.2D. In App.jsx, create routes to match the UI routes of your Flask application (not the API routes). The example below uses simple text as a monkey patch for each route.
Step 3.2: Redirect Unrecognized Routes to Home. Redirect all unrecognized routes to the "home" route. There is a simpler, cleaner way to do this than was taught in Exercise 5.2D "useNavigate Hook and Catch-All Routes", which was designed to teach specific features. React Router has a built-in component called Navigate that does the same thing:
Include the import with others from react-router-dom; use as follows:
The "replace" keyword avoids putting the redirected route in the Browser's history for purposes of the back and forward buttons.
The optional component below displays your Flask pages using an AJAX include (instead of using text as a monkey patch). React is designed to avoid AJAX includes or anything else that inserts HTML directly from a string, but React's "dangerouslySetInnerHTML" prop allows it.
To use it, use import FlaskPatch from './FlaskPatch.jsx', then replace <Route path="route" element="text" /> with <Route path="route" element=<FlaskPatch /> /> for any route.
Sidenote: It may not be obvious from the example above, but React is fairly finicky about the way async functions are used. React will make errors if getpage is not defined separately before being called in useEffect. React will make errors if getpage is defined outside the scope of the component or before the creation of setContent. React will make errors if getpage is not called in curly braces, forcing useEffect to return a promise. All of these issues basically arise from the fact that certain features in React are meant to be synchronous.