• Non ci sono risultati.

Frontend Development of Cardiofilo

3.2 Framework for user interface creation

3.2.1 React

React (also known as React.js or ReactJS) is a JavaScript library for creating user interfaces. It can be used as a basis in the development of single page or mobile applica-tions.

In an MVC context (Model-View-Controller, an architectural pattern capable of separat-ing the presentation logic of the data from the business logic) React therefore acts as a support for the creation of views, possibly generated from a data model. The data model consists of React components that are used to create the DOM elements, which constitute the interface, based on the data contained in JavaScript objects.

The components are self-sufficient, independent micro-entities that describe a part of the user interface. An application’s user interface can be divided into smaller components, where each component has its own code, structure and API (Application Programming Interface). The component architecture allows you to consider each piece in isolation.

Each component can update everything within its reach without being concerned about how it affects the other components. The components are also reusable and need data to work on. There are two different ways to combine components and data (see 3.6): either props or state. Props and state determine what it is the behaviour of the component renders.[12]

Figure 3.6: General scheme relating to the operation of React

Props:

Props (short of properties): constitute immutable and top-down values for which no al-teration is foreseen, useful for example to configure the component or to indicate default values for the status. This means that a parent component can pass any type of data it wants to its children as props, but the child component cannot modify its props.

A virtually infinite number of properties can be passed to a React component; in the component code it will be possible to read their values at any time by accessing the props object, and it will not be possible to modify them.

State:

It is a set of data that, if modified, results in an update of the component itself. Its scope is limited to the current component. A component can initialize its state and update it whenever necessary. The state of the parent component usually ends up being the props of the child component. When the state is passed outside the current scope, we refer to it as props.

The choice of passing information to the components in the form of properties or placing them in the state depends first of all on the variability expected for the information and, more generally, on the conceptual design on which one relies on to create one’s own Web application.

A React component can be of two types: a class component or a functional component.

Functional components:

Functional components are just JavaScript functions. They take an optional input which is a props.

Class components:

Class components offer multiple functions. The main reason for choosing class components

React event management:

Event management allows to make the React component interactive, responding to user actions and updating the graphic interface accordingly, acting on the state of the compo-nent itself.

The state is represented by an object accessible in the component code through the state property which can be initialized as desired; unlike what happens with the properties, provided by the props object, the status can be changed by calling the setState () method.

Following a change in status, generally through the management of an event that occurs with a user action, React invokes the render () function and verifies the need to make changes to the part of the DOM that constitutes the representation of the component within the web page.

The getInitialState () function is responsible for returning a JavaScript object that con-tains the initial state of the component; in the object it is possible to include all the properties we want according to the requirements of the desired components.

Virtual DOM:

Using React it is not necessary to use the DOM functions to modify the elements of the page: it is sufficient to change the information contained within the object representing the data model, represented by the state (state) in React. React uses a different system, from the other Javascript libraries, to track the changes, using the so-called Virtual DOM, that is to say a virtual representation of the structure of the page stored in memory and very similar to the original DOM, of which it can be seen as an abstraction. When an event occurs and it is necessary to "react" to it by changing the elements of the page, React first applies these actions to the Virtual DOM. By analyzing the differences between the status of the Virtual DOM prior to the occurrence of the event and the new one obtained by applying the changes, React determines the actual changes to be made to the real DOM.

The calculation of the differences between the two states of the virtual DOM is extremely fast, and thanks to it the interventions on the real DOM, tendentially slower, are limited to the bare minimum, thus guaranteeing excellent performance.

In the Virtual DOM there are both complex components like the classic elements of the HTML standard, such as a simple <div>. The management of the Virtual DOM and its transformations is completely the responsibility of React: the developer remains com-pletely transparent and no use is required API (Application Programming Interface) for its manipulation, as happens in the direct use of the real DOM, which should not be used. The abstraction represented by the Virtual DOM also allows to take advantage of a language independent of the browser, in which it is possible to express the characteris-tics of the component related to its appearance and behavior regardless of the browser in which the application will be hosted, leaving React the task of translating the developer’s

intentions into calls to JavaScript functions - both global and DOM functions - according to their actual availability (feature detection) within the browser of the user.

Finally, there are also server-side implementations of React’s Virtual DOM which, by us-ing the same functions and thus writus-ing code completely analogous to the client version, allow to return HTML pages generated directly by the server. To conclude, Virtual DOM is therefore of fundamental importance in the functioning of React.

Use of Node.JS with React:

Node.js is a runtime for the execution of JavaScript code based on the V8 engine, the same on which the Google Chrome JavaScript interpreter is based, famous in particular for its performance. It is an open source product available for all popular operating sys-tems and can be downloaded for free. In this case, Node.js is used locally, to enhance the development environment with the aim of producing and optimizing HTML pages, style sheets and JavaScript scripts which can then be hosted on any type of server (or hosting).

The installation of Node.js aims to create a development environment consisting of dif-ferent tools and services written in JavaScript and designed to work thanks to the Node runtime.

Node.js, therefore, becomes a fundamental component to support the development phase of the web application. Thanks to the support of Node and its package manager npm, it is possible to install additional packages and import the Babel transpiler to read files with JS syntax and create standard JavaScript files, interpretable by any browser, taking advantage of the features of the most modern versions of the language such as ES6.

Documenti correlati