Converting a static page or site into React App can be simpler than what some might think. The gist is that you'll only restructure and format things out. Just plug the data to it, or with an API to make it Dynamic or feel more like an App.
In this fast blog, I'll be going through the straightforward strides on the most proficient method to change over a static page into a React App.
Essential
This expects you as of now have React introduced into your machine, and obviously, you know the substance of the library.
So before we start the transformation, let's do a simple create-react-app to initialize our project. I'll utilize npx here:
npx create-react-app My_App
# wait for it to finish...
cd My_App # or code My_App if you have VS code
Remove the unnecessary files and the boilerplate code. Now we're ready to go.
Conversion
Turn page/s into Parent Components
If you only have one page, you can create a folder called components under the src folder. Then, create a single .jsx file there like index.jsx. Create a React Component within index.jsx, then copy and paste the body of your static HTML file to the return statement of that Component.
And if you have multiple pages, I recommend creating a separate pages folder under src folder and create .js file for each (HTML) page of your static site. Again, create React components for each file (or page) and copy-paste the body of the HTML files into the react components.
Fix Syntax
The next thing we'll be doing is correct the syntax of our plain HTML code into JSX. More specifically, change the following:
- change class to className
- change style props from string to objects, and change the BBQ-casing of CSS props to camelCase (i.e. style="text-align: center;" to style={textAlign: 'center'}).
- End the self-closing tags (i.e. <img> to <img />, or <br> to <br />.
0 Comments