A Beginner’s guide to getting started with React

A Beginner’s guide to getting started with React

React is a JavaScript library that is essentially used to build UI components. This platform can be utilized to develop applications and is highly beneficial for developers. User interfaces can be developed for mobiles, desktop platforms, and the web. React makes the whole process of developing apps a lot easier and renowned companies like Netflix, Airbnb, and many more make use of it. This outlines the popularity and the satisfaction drawn by React.

With this guide, we’ll be clearing out the basic concepts used in React. By the end of this article, you’ll be able to work with React on a step-by-step basis, enabling you to proceed further conveniently.

Basic Requirements for React

Before we proceed, there are a few basic requirements users would require to benefit from using React. Therefore, it’s essential for you to have some basic knowledge of the following fields: HTML, CSS, JavaScript, Node.js, and npm package manager. You’re required to install both Node and npm on your computer in order to proceed conveniently.

React Components

Unlike frameworks like Vue and Angular, React is used to deal with the view layer. That is why you will be needing additional libraries to manage processes like routing and state management. Applications developed using React are made using reusable UI components, which can be curated to interact with one another.

React components can either be classified into class-based components or functional components. The class-based components are declared using ES6 classes while the function components are declared using a function that returns some JSX. You do not have to rewrite the existing code to develop new features. React helps you get new features with ease.

Amongst these features, React Native is the most preferred one. It lets developers power mobile applications and renders them on the server with the use of Node. The stateless component, an essential component of React, assists with displaying the data received from the parent react component. In case any inputs are received, it simply needs to transfer them to the parent component.

Similarly, React’s stateful component is known to maintain the state of an application by responding with inputs. This involves responsibilities like fetching data from external sources and even tracking the user’s logging information. A useful tip to keep in mind: try to write stateless components as much as possible. This is because if needed, they are much easier to reuse while working on your overall applications or projects.

Understanding React Virtual DOM

Every time a new element is added to the UI, a virtual DOM will be created. Every UI in React is a component, each of which has a state. React makes a note of the patterns and observes changes in the states. With each change, the virtual DOM tree in React is updated, which then compares versions through a process called ‘diffing’. The virtual DOM enhances the performance of every function in React.

react virtual DOM

Thus, react ensures that all the updates are rendered at a stretch with a time of 60 frames per second. This will further ensure that there is no lag while using it.

Starting a Blank project on React

You must have set up a Node environment by now after following the basic requirements. Furthermore, make sure that you have the updated version of npm. Firstly, Develop your React native app. For this, you have to use the option, ‘Create React App’, which is the official script for building single-page applications on React.

Install the following:

npm i -g create-react-app

For creating a new React application, use this:

create-react-app message-app

If this is your first time running the above command, it might take some time depending on your internet speed. Other packages get installed simultaneously; these are required for setting up a development environment.

Once the project has set up, input the following commands to launch your application:

cd message-app

npm start

The following output will appear, ‘Compiled successfully’ and you can now view your react-app in the browser.

The default browser will automatically launch and once you have confirmed that your started project is running without any errors, you can open the folder using any code editor. You could start with the file package.json:

For Example:-


{
"name": "message-app", 
"version": "0.1.0",
"private": true, 
"dependencies": { 
"@testing-library/jest-dom": "^4.2.4",  
"@testing-library/react": "^9.3.2", 
"@testing-library/user-event": "^7.1.2", 
"react": "^16.13.1", 
"react-dom": "^16.13.1", 
"react-scripts": "3.4.3"
};
"scripts": { 
"start": "react-scripts start", 
"build": "react-scripts build", 
"test": "react-scripts test", 
"eject": "react-scripts eject" 
}, 
"eslintConfig": { 
"extends": "react-app" 
 }, 
"browserslist": { 
"production": [ 
">0.2%", 
"not dead", 
"not op_mini all" 
], 
"development": [ 
"last 1 chrome version", 
"last 1 firefox version", 
"last 1 safari version" 
] 
} 
} 

After doing so, you’ll notice that the ‘Create React App’ has installed many dependencies. The first three are related to the React Testing Library, then there are react and react-dom as well as react-scripts. The 4 npm scripts, start, build, test, and eject, which are used to perform repetitive tasks automatically, are included as well. A distinct difference between the original code and the development environment is provided by the tool Create React App.

When you run, ‘npm run eject’, the tool will stop hiding the inside functions and will include all of them in your project file. However, this isn’t recommended even though it’ll provide you with control over the dependencies. You ask us why? It’s because you will have to look after all the complex codes and manage them. If you ever come across such an instance, customize-cra to configure the build process without having to eject.

A great characteristic of React is that it provides automatic reloading. This means that alterations made to any code will be refreshed automatically on your browser. Moreover, CSS changes will update the DOM without refreshing. You can stop the development process with Ctrl + C. To proceed, delete everything except serviceWorker.js, setupTests.js from the src folder.

How to write code on React?

It’s finally time to discuss how to write codes on React. You can find the src folder inside the messaging app which you’ve curated. Input the code shared below once you’ve completed curating the index.js file:

For Example:-

    
import React from ‘react’;
import ReactDOM from ‘react-dom’;
ReactDOM.render(

Hello World

, document.getElementById(‘root’));

Using npm start or yarn start, you can start the development server again. You will get the output, ‘Hello World’, displayed on your browser. This is one of the most basic examples.

For React components to be rendered, the index.js file is the root. After getting to know the basics of coding in React, you can move further in creating a React component. To create a react component, create a file named App.js inside the src folder and input the following code:

For Example:-

    
import React, { Component } from ‘react’;
class App extends Component {
render(){
return (
Hello, World Again!
) } } export default App;

You must create a React component by defining a class of JavaScript, which is a subclass of React Component. You can then style JSX elements in different ways, a few common ones include JSX inline styling and external stylesheets.

Bottom Line

This guide is just an introductory version on how to go about with React. You can explore React in its depths once you’re well versed with your basics and have your concepts clear. There are several features like React hooks, component reuse, prop checking, data fetching, debugging, handling of errors, routing, and more that need to be explored. Once you get the hang of using React, you’ll understand the simplicity and convenience it offers.

Recent Comments:-

There are no comments yet.

Leave message