In this post, we breakdown the best practices for developing single-page applications with React - from file structure to performance to testing.
React is an excellent choice for building single-page applications. Developed by Facebook and now widely adopted, React is our preferred technology choice for building single-page applications due to its excellent performance and clean component-based architecture.
React structures the user interface into a set of small individual components, each having their own isolated state. The structure makes it very easy to develop reusable components and test them.
One of the most significant advantages of developing applications using React is performance. Rather than manipulate the elements of the page directly, React works with an in-memory representation of the page (the “Virtual DOM”) before copying the changes to the actual HTML tree, delivering much faster user experience.
Another advantage is declarative views, which make the code extremely easy to understand. In react, a component is simply a JavaScript function which takes input data (model) and returns what to display (view).
The practices we use for developing React applications are as follows
When approaching the development of a new React application, we start with reviewing wireframes and proposed layouts to identify logical blocks. While these will include the navigation, header and footer areas, we also look at the information presented in cards, lists, galleries, buttons and the like.
We split these blocks into smaller and smaller pieces (components). For example, the individual cards contained within a list view are likely to include images, icons and buttons.
To help determine when a component should be broken down further, we review whether that component can be reused in another place or used with other data.
Typically we use components to represent individual items (such as cards, sitemap) and critical elements within those cards (such as images, icons, buttons).
We take a modular and highly-structured approach to React applications. The practices we use to organise React applications are as follows:
The approach makes it very easy to understand everything used by each component.
A single page application depends on data which can come from many different sources (API endpoints, user inputs, device hardware, etc.). A vital part of the design is to decide how to store and best manage this data.
We load data into the redux store if:
All authentication information is also stored in the redux store, because any component might rely on this, checking roles/authorisation before showing specific data.
The data should be in a local state of the component if only this component uses this data. For example, the following data are a good candidate to be stored in the local state:
By default, React will re-render all components, even when data has not changed. To improve performance, and reduce unnecessary calculation we design our applications to track when components need to be updated – via shouldComponentUpdate()
For more information, see: https://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate and https://facebook.github.io/react/docs/optimizing-performance.html
To support our practices, we use extensive automation, helping to deliver a React application quickly and effectively
Tell us what you would do with a precise digital strategy, improved digital presence, and accelerated growth.