React tutorial
About the tutorial
This short note is based and created assuming you are using a Linux (debian/ubuntu) system, most of the commands will run on other systems like Windows and Mac as well but may require some changes. In case you need to follow along and using a Windows system, you can use the Windows Subsystem for Linux to run the commands.
Installing react
In order to install react, node and npm are required. To install node and npm, run the following command:
Note: The above command may install a deprecated version of node and npm. To install the latest version of node and npm, run the following commands:
To verify the installation run the following commands
If installed sucessfully you should see the version of node and npm installed.
In order to install react, run the following command:
|
|
To verify the installation run the following command
|
|
Creating and running a react app
To create a react app, run the following command:
|
|
This will create a react app named my-react-app
in the current directory. To run the app, run the following commands:
This will start the react app and open it in the default browser. If it doesn’t open automatically, you can just click on the terminal link to open it in the browser.
File structure
Since we are using create-react-app, the file structure is already created for us.
|
|
node_modules/
conttains all the dependencies of the project. package.json
contains the information about the project and the dependencies. src/
contains the source code of the project. public
contains the static files of the project.
If we just cd
into the src/
directory, we can see the following files:
|
|
Why to use react?
React is a JavaScript library for building user interfaces. With the standard HTML, CSS and JavaScript the browser engine creates a DOM (Document Object Model), which is a tree of objects. However with the complezity of applications increasing, the DOM becomes more and more complex. Here react comes into play, wherein all are arranged into components and it also creates something called as a virtual DOM, which is a copy of the actual DOM.
Standard practices in react
- Components should be named with PascalCase
- All the react components must be in the
components/
directory. Source:/src/components
More about components
- A component can be created using a function or a class. However using a function is more common. A component can be created using the following syntax:
- A component can be dynamically rendered using the following syntax:
- A jsx or tsx function can only return one component. If more are required, they must be wrapped in a div or a fragment. A fragment can be created using the following syntax:
Creating a basic hello world app
Create a file named Message.js
in the src/
directory and add the following code:
Now in the App.js
file, add the following code:
Now to run the app use
|
|
About react and JSX: React uses JSX, which is a syntax extension to JavaScript. Both JSX and TypeScript can be used in React but using JSX is more common.
Dynamic content
Message.js
can be updated to the following to display dynamic content:Adding a button to the app
Using bootstrap
Bootstrap is a CSS framework that makes it easy to create responsive websites. To install bootstrap, run the following command
|
|
example
|
|
After installing bootstrap npm will automatically include it in the package.json
file. To use bootstrap, add the following line to the App.js
file.
contents of App.js
|
|
since we are using react its better to make a seperate file for each component. So create a file named ListGroup.js
in the src/
directory and add the following code:
|
|