React With Spring Boot Crash Course

React is a very popular javascript library for building user interfaces, that was originally developed by Facebook. React only deals with updating the view, so is depending on other frameworks for handing everything else.

Spring Boot is essentially an easy to setup java based environment that makes it simple to develop and deploy web applications based on the Spring framework.

Here we will look at creating an application with both React.js as the client UI and Spring Boot as the server.

React Components

A React UI is fundamentally built up of components. Components are how you define each part of the react UI that you see in the browser. Each of the components are built to be stand alone, and then brought together to create the complex UI you require.

A React UI will usually have a root or top level component that is the entry point for the starting point or entry point for the UI in React. This will be the parent component for other components that make up the React UI. For instance we can have lots of components that make up our UI and can be used by this page or many pages in the UI app.

Each component in a UI can be built seperately them integrated to build the compete UIs that we require.

React Class Components

A React component can be implemented as a javascript class component, that maintains its own state (the data), and presents that state in the UI using the render() method. The render() methods generates javascripts objects.

The DOM and the Virtual Dom

The DOM or Document Object Model is a way of accessing and describing elements in a document. The World Wide Web Consortium describe the DOM as

The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.

https://www.w3schools.com/js/js_htmldom.asp

In the brower, documents are represent as HTML, and the DOM is the object model and programming interface used to access the elements of the DOM in the browser. So every time we change something in the UI the DOM needs to be updated, which of course comes with a performance overhead. So if we need to update the UI regularly, updating the DOM will take some time.

Updating the DOM is dependent on how many elements you have in your UI, so of course the more elements in the UI, the slower it will be to update. To help with this, React maintains its own DOM, known as the virtual DOM.

React will use the virtual DOM to determine what has changed in the UI. Rather than updating the whole of the real DOM, React will use some intelligence to figure out which elements in the UI have actually changed, and thus only update the parts of the real DOM that have changed rather than the whole DOM, thus improving performance.

Why Is React So Fast?

The other benefit of not using the real DOM is that changes to the virtual DOM can be managed by the React library. This is how React can change the UI based on change to the state of the components.

Setting Up The Eclipse Maven Project For Spring Boot

Lets go to start.spring.io/ to get what we need for Spring Boot with Rest Repositories, Thymeleaf, JPA and H2 as dependencies. We might not need all of these but as we used them previously lets grab them for now then we can remove them later if needed.

We fill in the following values at start.spring.io/.

Click Generate to download a project zip file. Unpack the zip.

Open eclipse, create a new java project called sprintbootreact2.

Create your source and test folders, heres my standard setup below.

Copy the contents of the downloaded zip file into the project and yu should end up with this

If we look in src/main/java, we get a default spring application.

From the eclipse menu, select Run, Run Configurations, then scroll down to maven build.

Right click, new configuration and fill in the details. Click Apply, then Run.

REST Application Config

We arent doing anything with Spring Data REST at the moment, but lets adjust the URI for its REST endpoint in case we use it later.

Add the following property to the application.properties file already in the src/main/resources folder to adjust its default URI so it doesnt conflict with the UI we are going to build.

Modify the build path in the POM for the maven eclipse plugin to ensure that application.properties is included in the build path, by setting filtering = false.

Setting Up The Spring MVC Controller For The React UI

Create a html page for the React components to live in. We do this by defining a Spring MVC controller and defining a Thymeleaf HTML template.

The Spring MVC Controller

The @Controller marks this as a Spring MVC controller, the request mapping maps slash to the index method and index is the prefix of the html page to use.

The index.html Thymeleaf template

The index.html template needs to resides in src/main/resources/templates folder. The react div is where the React.js components will appear.

Installing Node.js and React.js Javascript Modules Using The Pom.xml

We need to use the frontend-maven-plugin to enable us to

  • Install node.js and npm package manager
  • Install the node.js modules configured in package.json
  • run webpack to compile all the JavaScript components into a single file

So lets add the plugin config for frontend-maven-plugin to our pom, and run the build. Spring Boot will server anything found in src/main/resources/static automatically, so thats how the main.css and bundle.js (built by webpack) will be found.

Heres the additional bits for the POM for the frontend-maven-plugin

we need to also create a package.json file to configure NPM

and also webpack.config.js to configure webpack.

also lets create the skeleton React.js app.js file

Lets run our maven eclipse build again

Success!

Spring Boot and create-react-app

Many of the React.js tutorials you see will reference setting up your React.js application with the create-react-app npm command line tool. As we are setting up React.js to run out of Spring Boot it would be nice if we could so that as part of the build process.

So what does create-react-app actually do? Seems like it create a package.json file, does some other checks, and then downloads the selected version of react-scripts. So if we download the latest version of react-scripts, we should have everything we need for more tutorials.

So ive added react-scripts to the package.json file, and rerun the eclipse build script

Editing React.js Code With Visual Studio Code

I going to try editing my React.js javascript code with Microsofts Visual Studio Code rather than Eclipse, as it has better support for React. Will see how I get on with this, as many of the tutorials on youtube use it.

As per tutorials, I have added the following extensions

Prettier by Esben Petersen

Simple React Snippets by Burke Holland

ive also enabled format on save, settings, search for “format on save” and set to true.

Developing Your React.js App

While you are working on the React.js UI ,you want to be able to update the js code and see those changes reflected in the UI as quickly as possible. To help with that, its easier to create another React.js app, and have a Node.js server running to serve the UI changes.

  • Download the latest node.js and install from https://nodejs.org/en/download/
  • open a command window
  • Run npm install -g create-react-app to setup the react app creator
  • Go to where you want to create the react app and run create-react-app <app_name>
  • Run cd <app_name>
  • Run npm start

So we now have a node.js instance running on port 3000 that we can use to check our React.js code without rebuilding Spring Boot each time.

These are the files that got created in the reacto app that i generated

Lets delete those and start from scratch, and also open the source folder in visual studio. If we delete everything from the src folder and create a new index.js, notice that the browser window will automatically reload. This is because an application thats been automatically generated with create-react-app will be setup to automatically compile and reload in the browser.

Leave a Comment

Your email address will not be published. Required fields are marked *