Fullstack Part1 - Introduction To React
Fullstack Part1 - Introduction To React
a
Introduction to React
We will now start getting familiar with probably the most important topic of this course,
namely the React library. Let's start by making a simple React application as well as getting
to know the core concepts of React.
The easiest way to get started by far is by using a tool called Vite .
Let's create an application called introdemo, navigate to its directory and install the libraries:
cd introdemo copy
npm install
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 1 of 21
:
The application is started as follows
The console says that the application has started on localhost port 5173, i.e. the address
http://localhost:5173/ :
Vite starts the application by default on port 5173. If it is not free, Vite uses the next free
port number.
Open the browser and a text editor so that you can view the code as well as the webpage at
the same time on the screen:
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 2 of 21
:
The code of the application resides in the src folder. Let's simplify the default code such that
the contents of the file main.jsx looks like this:
ReactDOM.createRoot(document.getElementById('root')).render(<App />)
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 3 of 21
:
The files App.css and index.css, and the directory assets may be deleted as they are not
needed in our application right now.
Component
The file App.jsx now defines a React component with the name App. The command on the
final line of file main.jsx
renders its contents into the div-element, defined in the file index.html, having the id value
'root'.
By default, the file index.html doesn't contain any HTML markup that is visible to us in the
browser:
You can try adding there some HTML to the file. However, when using React, all content that
needs to be rendered is usually defined as React components.
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 4 of 21
:
As you probably guessed, the component will be rendered as a div-tag, which wraps a p-tag
containing the text Hello world.
() => ( copy
<div>
<p>Hello world</p>
</div>
)
There are a few ways to define functions in JavaScript. Here we will use arrow functions ,
which are described in a newer version of JavaScript known as ECMAScript 6 , also called
ES6.
Because the function consists of only a single expression we have used a shorthand, which
represents this piece of code:
The function defining the component may contain any kind of JavaScript code. Modify your
component to be as follows:
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 5 of 21
:
return (
<div>
<p>Hello world</p>
</div>
)
}
Let us repeat this together: I promise to keep the console open all the time during this course,
and for the rest of my life when I'm doing web development.
return (
<div>
<p>Hello world, it is {now.toString()}</p>
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 6 of 21
:
<p>
{a} plus {b} is {a + b}
</p>
</div>
)
}
Any JavaScript code within the curly braces is evaluated and the result of this evaluation is
embedded into the defined place in the HTML produced by the component.
Note that you should not remove the line at the bottom of the component
The export is not shown in most of the examples of the course material. Without the export
the component and the whole app breaks down.
Did you remember your promise to keep the console open? What was printed out there?
JSX
It seems like React components are returning HTML markup. However, this is not the case.
The layout of React components is mostly written using JSX . Although JSX looks like HTML,
we are dealing with a way to write JavaScript. Under the hood, JSX returned by React
components is compiled into JavaScript.
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 7 of 21
:
}
The compilation is handled by Babel . Projects created with Vite are configured to
compile automatically. We will learn more about this topic in part 7 of this course.
It is also possible to write React as "pure JavaScript" without using JSX. Although, nobody
with a sound mind would do so.
In practice, JSX is much like HTML with the distinction that with JSX you can easily embed
dynamic content by writing appropriate JavaScript within curly braces. The idea of JSX is
quite similar to many templating languages, such as Thymeleaf used along with Java Spring,
which are used on servers.
JSX is " XML -like", which means that every tag needs to be closed. For example, a newline is
an empty element, which in HTML can be written as follows:
<br> copy
Multiple components
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 8 of 21
:
</div>
)
}
We have defined a new component Hello and used it inside the component App. Naturally, a
component can be used multiple times:
NB: export at the bottom is left out in these examples, now and in the future. It is still
needed for the code to work
Writing components with React is easy, and by combining components, even a more
complex application can be kept fairly maintainable. Indeed, a core philosophy of React is
composing applications from many specialized reusable components.
Another strong convention is the idea of a root component called App at the top of the
component tree of the application. Nevertheless, as we will learn in part 6 , there are
situations where the component App is not exactly the root, but is wrapped within an
appropriate utility component.
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 9 of 21
:
}
Now the function defining the component has a parameter props. As an argument, the
parameter receives an object, which has fields corresponding to all the "props" the user of
the component defines.
There can be an arbitrary number of props and their values can be "hard-coded" strings or
the results of JavaScript expressions. If the value of the prop is achieved using JavaScript it
must be wrapped with curly braces.
Let's modify the code so that the component Hello uses two props:
return (
<div>
<h1>Greetings</h1>
<Hello name='Maya' age={26 + 10} />
<Hello name={name} age={age} />
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 10 of 21
:
</div>
)
}
The props sent by the component App are the values of the variables, the result of the
evaluation of the sum expression and a regular string.
Component Hello also logs the value of the object props to the console.
I really hope your console was open. If it was not, remember what you promised:
I promise to keep the console open all the time during this course, and for the rest of
my life when I'm doing web development
Software development is hard. It gets even harder if one is not using all the possible
available tools such as the web-console and debug printing with console.log .
Professionals use both all the time and there is no single reason why a beginner should not
adopt the use of these wonderful helper methods that will make their life so much easier.
Depending on the editor you are using, you may receive the following error message at this
point:
It's not an actual error, but a warning caused by the ESLint tool. You can silence the warning
react/prop-types by adding to the file eslint.config.js the next line
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 11 of 21
:
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
parserOptions: {
ecmaVersion: 'latest',
ecmaFeatures: { jsx: true },
sourceType: 'module',
},
},
settings: { react: { version: '18.3' } },
plugins: {
react,
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...js.configs .recommended.rules,
...react.configs .recommended.rules,
...react.configs ['jsx-runtime'].rules,
...reactHooks.configs .recommended.rules,
'react/jsx-no-target-blank': 'off',
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
'react/prop-types': 0,
},
},
]
Some notes
React has been configured to generate quite clear error messages. Despite this, you should,
at least in the beginning, advance in very small steps and make sure that every change
works as desired.
The console should always be open. If the browser reports errors, it is not advisable to
continue writing more code, hoping for miracles. You should instead try to understand the
cause of the error and, for example, go back to the previous working state:
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 12 of 21
:
As we already mentioned, when programming with React, it is possible and worthwhile to
write console.log() commands (which print to the console) within your code.
Also, keep in mind that First letter of React component names must be capitalized. If you
try defining a component as follows:
the page is not going to display the content defined within the footer component, and
instead React only creates an empty footer element, i.e. the built-in HTML element instead
of the custom React element of the same name. If you change the first letter of the
component name to a capital letter, then React creates a div-element defined in the Footer
component, which is rendered on the page.
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 13 of 21
:
Note that the content of a React component (usually) needs to contain one root element. If
we, for example, try to define the component App without the outermost div-element:
Using a root element is not the only working option. An array of components is also a valid
solution:
However, when defining the root component of the application this is not a particularly wise
thing to do, and it makes the code look a bit ugly.
Because the root element is stipulated, we have "extra" div elements in the DOM tree. This
can be avoided by using fragments , i.e. by wrapping the elements to be returned by the
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 14 of 21
:
component with an empty element:
return (
<>
<h1>Greetings</h1>
<Hello name='Maya' age={26 + 10} />
<Hello name={name} age={age} />
<Footer />
</>
)
}
It now compiles successfully, and the DOM generated by React no longer contains the extra
div element.
Consider an application that prints the names and ages of our friends on the screen:
return (
<div>
<p>{friends[0]}</p>
<p>{friends[1]}</p>
</div>
)
}
However, nothing appears on the screen. I've been trying to find a problem in the code for
15 minutes, but I can't figure out where the problem could be.
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 15 of 21
:
I promise to keep the console open all the time during this course, and for the rest of
my life when I'm doing web development
The core of the problem is Objects are not valid as a React child, i.e. the application tries to
render objects and it fails again.
<p>{friends[0]}</p> copy
and this causes a problem because the item to be rendered in the braces is an object.
In React, the individual things rendered in braces must be primitive values, such as numbers
or strings.
return (
<div>
<p>{friends[0].name} {friends[0].age}</p>
<p>{friends[1].name} {friends[1].age}</p>
</div>
)
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 16 of 21
:
}
So now the friend's name is rendered separately inside the curly braces
{friends[0].name} copy
and age
{friends[0].age} copy
After correcting the error, you should clear the console error messages by pressing and
then reload the page content and make sure that no error messages are displayed.
A small additional note to the previous one. React also allows arrays to be rendered if the
array contains values that are eligible for rendering (such as numbers or strings). So the
following program would work, although the result might not be what we want:
return (
<div>
<p>{friends}</p>
</div>
)
}
In this part, it is not even worth trying to use the direct rendering of the tables, we will come
back to it in the next part.
Exercises 1.1.-1.2.
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 17 of 21
:
The exercises are submitted via GitHub, and by marking the exercises as done in the "my
submissions" tab of the submission application .
The exercises are submitted one part at a time. When you have submitted the exercises for
a part of the course you can no longer submit undone exercises for the same part.
Note that in this part, there are more exercises besides those found below. Do not submit
your work until you have completed all of the exercises you want to submit for the part.
You may submit all the exercises of this course into the same repository, or use multiple
repositories. If you submit exercises of different parts into the same repository, please use a
sensible naming scheme for the directories.
One very functional file structure for the submission repository is as follows:
part0 copy
part1
courseinfo
unicafe
anecdotes
part2
phonebook
countries
For each part of the course, there is a directory, which further branches into directories
containing a series of exercises, like "unicafe" for part 1.
Most of the exercises of the course build a larger application, eg. courseinfo, unicafe and
anecdotes in this part, bit by bit. It is enough to submit the completed application. You can
make a commit after each exercise, but that is not compulsory. For example the course info
app is built in exercises 1.1.-1.5. It is just the end result after 1.5 that you need to submit!
For each web application for a series of exercises, it is recommended to submit all files
relating to that application, except for the directory node_modules.
The application that we will start working on in this exercise will be further developed in a few
of the following exercises. In this and other upcoming exercise sets in this course, it is enough
to only submit the final state of the application. If desired, you may also create a commit for
each exercise of the series, but this is entirely optional.
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 18 of 21
:
Use Vite to initialize a new application. Modify main.jsx to match the following
ReactDOM.createRoot(document.getElementById('root')).render(<App />)
return (
<div>
<h1>{course}</h1>
<p>
{part1} {exercises1}
</p>
<p>
{part2} {exercises2}
</p>
<p>
{part3} {exercises3}
</p>
<p>Number of exercises {exercises1 + exercises2 + exercises3}</p>
</div>
)
}
and remove the extra files App.css and index.css, also remove the directory assets.
Unfortunately, the entire application is in the same component. Refactor the code so that it
consists of three new components: Header, Content, and Total. All data still resides in the
App component, which passes the necessary data to each component using props. Header
takes care of rendering the name of the course, Content renders the parts and their number
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 19 of 21
:
of exercises and Total renders the total number of exercises.
return (
<div>
<Header course={course} />
<Content ... />
<Total ... />
</div>
)
}
WARNING Don't try to program all the components concurrently, because that will almost
certainly break down the whole app. Proceed in small steps, first make e.g. the component
Header and only when it works for sure, you could proceed to the next component.
Careful, small-step progress may seem slow, but it is actually by far the fastest way to
progress. Famous software developer Robert "Uncle Bob" Martin has stated
that is, according to Martin, careful progress with small steps is even the only way to be fast.
Refactor the Content component so that it does not render any names of parts or their
number of exercises by itself. Instead, it only renders three Part components of which each
renders the name and number of exercises of one part.
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 20 of 21
:
Our application passes on information in quite a primitive way at the moment, since it is
based on individual variables. We shall fix that in part 2 , but before that, let's go to part1b to
learn about JavaScript.
About course
Course contents
FAQ
Partners
Challenge
https://fullstackopen.com/en/part1/introduction_to_react 21/06/2025, 13 00
Page 21 of 21
: