React setup (from scratch!)
going from nothing to a boilerplate for your future React projects
Many tutorials mention create-react-app
to beginners of React, which often includes redundancies and intimidating stuff on the side ... however, another way exists! For a more bare-bones approach, we can follow this "big bang to life" procedure:
initialize the project with a
package.json
fileinstall a few dependencies (including
react
)create the foundational
public/index.html
filecreate our foundational
index.js
entry point filecreate our
App
filecreate other component files
Initializing the project
This applies not only to React projects but to any web project!
On a new folder, e.g. jonotype
, we will enter this following command on Terminal:
This creates a file named package.json
that describes our new web app:
We can do a few modifications:
Updating the entry point
We update the main
property from index.js
to src/index.js
just to keep our files more organized!
Adding npm run
scripts
npm run
scriptsWe then include our npm run
scripts - these enable us to run a local version of our app (webpage)!
Adding browserslist
browserslist
The browserslist
property just excludes any ancient browsers like Internet Explorer!
Installing the dependencies
Now, we can install three dependencies (for now) which allow us to use the React library and family of scripts:
Upon installation our package.json
automatically updates to something like this:
(The dependencies
property adds in react
+ react-dom
+ react-scripts
!)
The foundational HTML file
Once we let those dependencies install, we can create a new folder called public
, in which we can place our foundational index.html
file:
Briefly, this minimal HTML file consists of:
the
<head>
tag for technical meta datacharacter set
browser sizing
<title>
bar text
the
<body>
tag which has<noscript>
warning because React apps require JavaScript (which everyone should have enabled on their browsers)<div id="root"></div>
the most important part here, where the React app lives!
The foundational index.js file
Now, we will connect the foundational HTML file with a foundational JavaScript file; this src/index.js
file also happens to import the React scripts:
Note that errors will occur because we have not created the App
code yet, which will do in the next section!
Also note:
StrictMode
is a built-in component of React which outputs errors in the browser's console concerning any problems inside the<StrictMode></StrictMode>
tagcreateRoot
builds upon the DOM inindex.html
The App.js file
Finally, let's get some real visible code going by starting a new file src/App.js
:
Breaking that down:
export
means we can use this file in other filesdefault
means that when weimport
this file elsewhere we do not have to call itApp
:
Other component files
Creating child components keeps our code more organized and "modular", so let's give this a try with src/Component.js
, with CamelCase
as the convention for component files:
Notice how we did not say export default function Component()
there!
Then, going back to src/App.js
we then import
and render Component.js
:
Some things to notice here:
We use curly braces for
{ Component }
to extract the functionComponent
fromsrc/App.js
We cannot replace
Component
with another name because we did not specify it as anexport default
back insrc/Component.js
Sometimes, specifying a
default
in a child component helps if we wish to rename the child component later on
Aliasing the imported component
If we want an imported component to have a different name, then we could still give it an alias using the as
operator in the import
statement:
Oftentimes, we would need to do this if there exist naming conflicts!
Running our app
Finally, we can run our app! In Terminal, we would simply run the following command:
We should see something like:
Then, from our browser, we go to localhost:3000
and we should see something like:
Notice the relationship of components
"A child component!" lives in
src/Component.js
src/Component.js
gets imported bysrc/App.js
src/App.js
gets imported bysrc/index.js
src/index.js
builds uponpublic/index.html
Clearly, the powerful implications of React components already appear in this very basic static "app" in terms of:
re-usability
we could include
<Component />
as many times in as many places as we wish, insrc/App.js
division of labour
if we want to expand upon
src/Component.js
we could do so without affecting the rest ofsrc/App.js
portability
keeping things in smaller components makes code easier to read
Moving on
We have covered quite a bit here, starting from nothing and ending with a basic static app - but more exciting topics lie ahead! More, such as:
building the app
deploying the app to the internet
creating dynamic components
building the illusion of a multi-page website through routes
Developers never get bored!
By the way, you can see the final product in https://github.com/joncoded/jonotype
Last updated